Every time I try and run my python code in the terminal I always get something like this,
Hello World
Enter your name: Tyler
Traceback (most recent call last):
File "HelloWorld.py", line 3, in <module>
name = input('Enter your name: ')
File "<string>", line 1, in <module>
NameError: name 'Tyler' is not defined
I am new to Python so please forgive me, I usually program in c# but Windows broke so I am trying to learn python.
Here is my code:
print('Hello World')
name = input('Enter your name: ')
print('Hi', name)
age = input('Enter your age: ')
age = int(age)
if (age == 35):
print('You are as old as Derek Banas')
if (age == 19):
print('You are the same age as me!')
else:
print('You are a different age than me')
print('Hello', name, 'You are', age, "It's nice to see you again!")
4 Answers 4
You should use raw_input instead of input.
3 Comments
from __future__ import print_function)input should work just OK on Python 3.x.You seem to be using python 3 so to run it from terminal you need to use python3 script.py instead of python script.py
6 Comments
python3 script.py, use input instead of raw_input.Thank you all for being patient with me, here is the solution I have found based on everyones input! I was coding it in IDLE using Python 3.4 then just typing python in terminal. When I changed it to raw_input it fixed it but gave me an error running it in IDLE.
Here is the solution I have found: When I use cd Desktop/Python then running it from there I should use Python3 not python so the code will look like this:
cd Desktop/Python
python3 HelloWorld.py
That fixed my issue! Thanks everyone who helped!
Comments
name = input('Enter your name: ')
age = input('Enter your age: ')
instead use
name = raw_input('Enter your name: ')
age = raw_input('Enter your age: ')