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 Meet Python Input and Coding Style

thomas Sichel
thomas Sichel
8,992 Points

Error of input

current_mood= input("how are you today?  ")
how are you today?  Wonderful
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'Wonderful' is not defined

can you help please

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hey thomas Sichel, The issue is likely due to Python version.

In Python 2.x, the default [input}(https://docs.python.org/2/library/functions.html#input) function behavior is to assume the string receive is to be interpreted as code. Hence, Wonderful raises an error because there isn't a variable named Wonderful. If you use the input "Wonderful" (include quotes), then Python 2.x will assign this string to the variable current_mood. To get the "Python 3 behavior" in Python 2 use [raw_input()(https://docs.python.org/2/library/functions.html#raw_input) which automatically converts the response into a string.

In Python 3.x, the default input function behavior is to assume the string received is a proper string. If quotation marks are present then you get quotes embedded in the string.

$ python2
Python 2.7.18 (default, Mar  8 2021, 13:02:45) 
[GCC 9.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> current_mood = input("how are you today?  ")
how are you today?  Wonderful
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'Wonderful' is not defined
>>> current_mood = input("how are you today?  ")
how are you today?  "Wonderful"
>>> current_mood
'Wonderful'
>>>  (Ctrl-D)

$ python3
Python 3.9.0+ (default, Oct 20 2020, 08:43:38) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> current_mood = input("how are you today?  ")
how are you today?  Wonderful
>>> print(current_mood)
Wonderful
# using unnecessary quotes leads to quotes embedded in the string.
>>> current_mood = input("how are you today?  ")
how are you today?  "Wonderful"
>>> current_mood
'"Wonderful"'
>>> (Ctrl-D)

$ 

Post back if you need more help.