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 (2015) Letter Game App Letter Game Introduction

Guess 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?

Sorry, I don't know code looks like this in this page.

1 Answer

28,520 Points

All 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:

  1. The use of print as a function. For example print('_', end=''). In Python 2 print 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 of print by including this at the beginning of your Python file: from __future__ import print_function.

  2. The use of input. In Python 2 input from the input function is evaluated as Python code. To get input as a string you have to use raw_input instead. raw_input was renamed to input in Python 3 hence why that line works in Python 3.

Thank you sincerely.