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 trial

Python Python Basics (2015) Number Game App Number Game Refinement

Luis Bonilla II
Luis Bonilla II
11,481 Points

Why do I get this error when running the program in mac's terminal?

Traceback (most recent call last):
  File "number_game.py", line 35, in <module>
    game()
  File "number_game.py", line 28, in game
    play_again = input("Do you want to play again? Y/n: ")
  File "<string>", line 1, in <module>
NameError: name 'n' is not defined
Luis Bonilla II
Luis Bonilla II
11,481 Points

Here is the code ( note I get the same error with the instructors code too):

import random

def game():
    # generate a randomnumber between 1 and 10
    secret_num = random.randint(1, 10)
    guesses = []

    while len(guesses) < 5:
        try:
            # get a number gues from the player
            guess = int(input("Guess anumber between 1 and 10: "))
        except ValueError:
            print("{} isn't a number!".format(guess))
        else:       
            # compare guess to secret number
            if guess == secret_num:
                print("You got it! My number was {}".format(secret_num))
                break
            elif guess < secret_num:
                print("My number is higher than {}".format(guess))
            else:
                # print hit/miss
                print("My number is lower than {}".format(guess))
            guesses.append(guess)
    else:
        print("You didn't get it,! My number was {}".format(secret_num))

    play_again = input("Do you want to play again? Y/n: ")

    if play_again.lower() != 'n':
        game()
    else:
        print("Bye!")

game()

4 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Luis,

Thanks for posting your code.

What version of Python are you using (type python3 --version)?

Also, assuming you are using Python 3 (which you should be, Python 2 is very old and will soon be unsupported), you should ignore Joshua's suggestion which only applies to Python 2.

Your program works fine on my Mac (v3.6.0). However, Apple is notorious for using extremely old versions of Python on their systems. Even Apple recommends that you not use the versions they install.

If you're using an old version of Python then you should definitely install a newer version. Kenneth has done a video for installing Python 3.5 with Pip and Venv on the Mac.

Let me know if you have any issues.

Cheers

Alex

Luis Bonilla II
Luis Bonilla II
11,481 Points

Running Python 2.7.10, thought I was running the latest. Thanks!

Python's input is used to read integers. You should use raw_input if you want to read text (string) from a user.

Luis Bonilla II
Luis Bonilla II
11,481 Points

That did it. I'm just curious why the instructor's code worked in terminal like that. I ran this code with an iOS app called Pythonista and it ran with no issues but I get that error on the mac with mine or the instructor's code.

Luis Bonilla II
Luis Bonilla II
11,481 Points

So I did have the latest python installed just ran the incorrect command (python instead of python3). raw_input gives and error in the latest release of python