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 trialSURENDRA MALLA
3,727 Pointscode works in workspace but not on visual studio code
first_name = input("what is your name?")
print("hello,", first_name)
print(first_name, "is learning")
and when i run the script
surendras-MBP:python smalla$ python hello.py
what is your name?malla
Traceback (most recent call last):
File "hello.py", line 10, in <module>
first_name = input("what is your name?")
File "<string>", line 1, in <module>
NameError: name 'malla' is not defined
surendras-MBP:python smalla$
Mod Edit: Added code formatting to make the post easier to read.
6 Answers
Bruce Röttgers
18,211 PointsYour computer uses python 2 if you run python hello.py
in bash.
Your computer uses python 3 if you run python3 hello.py
in bash.
andren
28,558 PointsThis happens because you have Python 2 installed on your machine, while Treehouse teaches Python 3.
There were many changes between Python 2 and 3, and some of them were breaking changes that meant that Python 2 code won't always work in Python 3 and vice versa. That is why Python 2 is still the default Python version in many operating systems, including macOS. In Python 2 the input
method interpreted the input it got as code that it should execute, rather than simply returning it as a string like the method does in Python 3.
Python 2 does have a method that returns the input as a string called raw_input
, so if you change input
to raw_input
then you will no longer get a NameError
. But there are tons of other changes between Python 2 and 3 that will make your code behave differently than expected. So if you want to test out the code you learn on Treehouse on your own machine then it would be a good idea to install Python 3 on it.
Bruce Röttgers
18,211 PointsYou can install Python 3 from the official site python.org.
if you're on Mac and have Homebrew installed you can do brew install python3
@andren could you format the code in the question
andren
28,558 PointsI can indeed . Also yes I should have mentioned how to install Python 3, it slipped my mind while writing the answer so thank you for posting that .
SURENDRA MALLA
3,727 Pointsi did installed Python 3 and if i lunch python shell it is 3.7 but if i run it in terminal and visual studio code it runs Python 2.
Bruce Röttgers
18,211 PointsYou need to run the command python3 to use the python 3 interpreter.
SURENDRA MALLA
3,727 Pointsi am still not getting it, sorry. when you want to run script in a terminal bash '''surendras-MBP:python smalla$ python hello.py ''' and it uses Python 2 automatically
SURENDRA MALLA
3,727 PointsThanks Andren and Bruce.
SURENDRA MALLA
3,727 PointsCool and crystal clear Bruce, Thanks! '''first_name = input("what is your name?") print("hello,", first_name) print(first_name, "is learning")''' output: "'surendras-MBP:python smalla$ python3 hello.py what is your name?malla hello, malla malla is learning surendras-MBP:python smalla$'''
Bruce Röttgers
18,211 PointsBruce Röttgers
18,211 PointsPlease format your code using the mardown cheatsheet and the three backticks.