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 trialJenna Dalgety
7,791 PointsGetting NameError: name 'y' is not defined when selecting 'y' to continue game
Getting this complete error when running on my computer's terminal:
Traceback (most recent call last): File "number_game.py", line 38, in <module> game() File "number_game.py", line 31, in game play_again = input('> ') File "<string>", line 1, in <module> NameError: name 'y' is not defined
Works fine on the Treehouse terminal, so not sure where the disconnect is. Here's the code: import random
def game():
number = random.randint(1, 10)
count = 0
print('''
Welcome to the number guessing game!
I'm thinking of a number between 1 and 10.
You get 3 tries.
''')
while count < 3: try: guess = int(input('> ')) count += 1 except ValueError: print('Please enter a number') else: if guess == number: print('You got it! The number was {}'.format(number)) break elif guess < number: print('Too low!') else: print('Too High!') else: print('The number was {}. You lose.'.format(number))
print('Would you like to play again Y/N?') play_again = input('> ') if play_again.lower() == 'y': game() else: print('Thanks for playing!')
game()
Ryan S
27,276 PointsHi Jenna,
If you see the "Markdown Cheatsheet" it gives instructions on how to format code.
Sometimes people get confused by the backticks vs single quotes. The backtick key is usually the same key as the "~" symbol in the top left corner of your keyboard.
Alexander Davison
65,469 PointsYou have to include ```python and ``` around the code, like this:
```python
print("Hello")
```
The result is:
print("Hello")
1 Answer
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsHey Jenna Dalgety,
So I believe the problem you are having is something that isn't immediately clear when you are even slightly new to Python. There were quite a few changes between Python 2.x and Python 3.x. The fact that you only have issues on your PCs terminal but Treehouse works fine tells me that Treehouse is running Python 3 and you must be running Python 2.
To find out, in your Terminal you can type
python --version
The built-in input() method in Python 2 is different from Python 3. In Python 2, input tries to evaluate the user input as a Python expression. An expression being something like other Python code. In Python 3, input was changed to no longer try to evaluate, it works to get user input and store it as a string.
The method you want to use in Python 2 to get user input is raw_input which would be Python 3s input equivalent.
To show this in an example you can copy this script I added comments to show you whats happening.
# running in Python 2 Shell
# For both inputs I will enter this: float(2)
example_1_input = input('Example 1: ') # float(2)
example_2_input = raw_input('Example 2: ') #float(2)
print(example_1_input)
# output is:
# 2.0
# notice it actually evaluated THAT input as valid python code. uh oh!
print(example_2_input)
# output is:
# float(2)
Jenna Dalgety
7,791 PointsAh! Yeah, I am running Python 2.7 on my Mac. Thanks much!
Jenna Dalgety
7,791 PointsJenna Dalgety
7,791 PointsSorry, and not sure why it's not formatting my text for indents and line breaks... First question posted!