For a practice problem in my homework, i'm making a guessing game that starts off by asking for a number. I'm trying to implement a way that prints "invalid input" when given a string, but i get an error message. here is my code:
def get_input():
'''
Continually prompt the user for a number, 1,2 or 3 until
the user provides a good input. You will need a type conversion.
:return: The users chosen number as an integer
'''
guess=int(input("give me 1,2,3"))
while True:
if guess==1 or guess==2 or guess==3:
return guess
else:
print "Invalid input!"
guess=int(input("give me 1,2,3"))
I get this message when I put in a string such as
hello/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 /Users/bob/PycharmProjects/untitled/warmup/__init__.py
give me 1,2,3hello
Traceback (most recent call last):
File "/Users/bob/PycharmProjects/untitled/warmup/__init__.py", line 51, in <module>
get_input()
File "/Users/bob/PycharmProjects/untitled/warmup/__init__.py", line 43, in get_input
guess=int(input("give me 1,2,3"))
File "<string>", line 1, in <module>
NameError: name 'hello' is not defined
Process finished with exit code 1