1

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!")
asked Jan 13, 2016 at 2:49

4 Answers 4

1

You should use raw_input instead of input.

answered Jan 13, 2016 at 2:55
Sign up to request clarification or add additional context in comments.

3 Comments

Then it should work. I don't see any errors in his code. Maybe he's trying to run it on Python 2.7.
If he is trying with py2, the error should be referent to the print function. Or maybe he's importing it from py3k (i.e. from __future__ import print_function)
Could be it. input should work just OK on Python 3.x.
1

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

answered Jan 13, 2016 at 2:55

6 Comments

So i used this in my code and now IDLE is giving me an error. Is raw_input just something used for terminals or am I typing it wrong? By the way I like how you explained yourself!
Thank you. what error do you have ? raw_input is a python 2 function used to read strings it works only in python 2 if you have python 3 you should have no error to begin with
if you want to run python3 from terminal use python3 instead of just python
If you run it with python3 script.py, use input instead of raw_input.
William is right, once I used python3 instead of python it fixed it! Is there a way to change that by default?
|
1

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!

answered Jan 13, 2016 at 3:09

Comments

0
name = input('Enter your name: ')
age = input('Enter your age: ')

instead use

name = raw_input('Enter your name: ')
age = raw_input('Enter your age: ')
Paul Rooney
21.7k9 gold badges47 silver badges64 bronze badges
answered Jan 13, 2016 at 5:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.