0

My code is as follows:

else:
print("that is incorrect!")
choice = input("Would you like to (g)uess again, or (q)uit? ")
if choice == "g":
    guess == input("What animal is this? ")
elif choice == "q":
    game_running = False
else:
    print("That is not a valid option")
    choice = input("Would you like to (g)uess again, or (q)uit? ")

When I run it, it returns the following for input = g, q, or anything else.

Traceback (most recent call last):
File "Project1.py", line 102, in <module>
choice = input("Would you like to (g)uess again, or (q)uit? ")
File "<string>", line 1, in <module>
NameError: name 'g' is not defined

I have used this format of evaluating a user guess several times before, and have never had any issues. Any suggestions would be greatly appreciated! I am running pygame on a Mac OSX, and I set python off python3 to allow pygame to work more effectively. I have added:

from __future__ import division, absolute_import, print_function, unicode_literals

to make python able to use python3 functionality.

Sumner Evans
  • 8,551
  • 5
  • 29
  • 46
  • I know this isn't your main question -- but I don't think you want `guess == input("What animal is this? ")` -- using `==` is a logical comparison, not an assignment. – Chris Johnson Feb 27 '14 at 04:54

2 Answers2

1

In python 2, you should use raw_input() instead of input() to get un-evaluated input. That is, input() will evaluate the user input before returning it to you.

choice = raw_input("Would you like to (g)uess again, or (q)uit? ")

btw, the first two lines seems to have some indentation issue?

else:
print("that is incorrect!")

Edit:

If one day you want to switch to python 3, do remember that the raw_input() as mentioned here is renamed into input() in python 3. You'll have to use eval(input()) to get the old input() behavior (which is a bit dangerous in my opinion...)

cybeliak
  • 86
  • 3
0

There are two problems, one is else and another one I believe is: guess == input("What animal is this? "). Follow the change bellow!

else:
    print("that is incorrect!")
choice = input("Would you like to (g)uess again, or (q)uit? ")
if choice == "g":
    guess = input("What animal is this? ")
elif choice == "q":
    game_running = False
else:
    print("That is not a valid option")
    choice = input("Would you like to (g)uess again, or (q)uit? ")
Sharif Mamun
  • 3,275
  • 5
  • 30
  • 50