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 trialJUNPEN gai
687 PointsGuess game implement in 2.7 or 3.6
import random words=['apple', 'banana', 'orange', 'melon', 'lemon', 'pear', 'berry', 'tree', 'dish',] while True: start=input("enter or q ")
This is the code i wrote I find that in 2.7 this not gonna work but not 3.6.
Traceback (most recent call last): File "guess.py", line 13, in <module> start=input("enter or q ") File "<string>", line 1, in <module>
NameError: name 'a' is not defined
line 13 is the last line of the code I show above. Here is the report of 2.7 python, whenever i input a letter this will come out. But in 3.6 this code works. Anyone know what is the difference that makes this happen?
1 Answer
andren
28,558 PointsAll Python courses on Treehouse teaches Python 3. So the project is definitively intended for Python 3.
There are two things in the project that causes issues for Python 2:
The use of
print
as a function. For exampleprint('_', end='')
. In Python 2print
is not a function, but a statement. As such it cannot be passed arguments in the way shown in that code snippet. You could get around this issue by importing Python 3's version ofprint
by including this at the beginning of your Python file:from __future__ import print_function
.The use of
input
. In Python 2 input from theinput
function is evaluated as Python code. To get input as a string you have to useraw_input
instead.raw_input
was renamed toinput
in Python 3 hence why that line works in Python 3.
JUNPEN gai
687 PointsThank you sincerely.
JUNPEN gai
687 PointsJUNPEN gai
687 PointsSorry, I don't know code looks like this in this page.