Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Meet Python Input and Coding Style

thomas Sichel
thomas Sichel
8,985 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 67,464 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.