对于我的作业中的练习问题,我正在制作一个猜测游戏,首先要求一个数字.我正在尝试实现一种在给定字符串时打印"无效输入"的方法,但是我收到错误消息.这是我的代码:
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"))
Run Code Online (Sandbox Code Playgroud)
当我输入诸如的字符串时,我收到此消息
hello/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 /Users/bob/PycharmProjects/untitled/warmup/__init__.py
Run Code Online (Sandbox Code Playgroud)
给我吗 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 …Run Code Online (Sandbox Code Playgroud) python ×1