Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (Retired) Ins & Outs Basic I/O

Brian Fairlie
PLUS
Brian Fairlie
Courses Plus Student 126 Points

NameError: name 'SPAM' is not defined

When I try to follow along in Spyder's console, I receive the following:

input('What do you ike to eat?') What do you ike to eat?Spam Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'Spam' is not defined food = input('What do you like to eat?') What do you like to eat?SPAM Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<string>", line 1, in <module> NameError: name 'SPAM' is not defined food Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'food' is not defined

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hi Brian,

It most likely you are running spyder with Python 2.7. In Python2.7 the input is interpreted raw, so you would have to type "Spam" for it to see it as a string and not as a variable named Spam. In Python3.4 the input is always interpreted as a string, so spam become "spam" automatically.

According to the help(input) for Python2.7 and Python3.4:

Python 2.7 Help on built-in function input in module __builtin__:

input(...)

input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).

Python 3.4

Help on built-in function input in module builtins:

input(...)

input([prompt]) -> string

Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading.

Alan Wils
Alan Wils
197 Points

I got the same issue here is the answer I found on stackoverflow: http://stackoverflow.com/questions/4960208/python-2-7-getting-user-input-and-manipulating-as-string-without-quotations

Use raw_input() instead of input():

testVar = raw_input("Ask user for something.") input() actually evaluates the input as Python code. I suggest to never use it. raw_input() returns the verbatim string entered by the user

Turns out you need to use raw_input() instead of input() if you're learning on python 2.7 instead of 3.4 (rebel!)

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Brian.

Can you please paste your code here so that we may have a look?

This error code gives us an idea of what is going on, something may be wrong in the definition of food or SPAM as far as I understand.

Please paste your code and we will sort this out together.

Vittorio