【发布时间】:2016-01-01 07:56:08
【问题描述】:
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
'''
#pass # REPLACE THIS WITH YOUR CODE
n = input ("Enter the number 1,2 and 3? ")
while n > 0 and n < 4:
print("Invalid Input, give the number between 1 to 3")
n = input ("Enter the number 1,2 or 3? ")
return (n)
get_input()
我没有得到答案,它只是不工作,我正在寻找这样的答案,
Give me one of 1,2 or 3: sid
Invalid input!
Give me one of 1,2 or 3: 34
Invalid input!
Give me one of 1,2 or 3: -7
Invalid input!
Give me one of 1,2 or 3: 0
Invalid input!
Give me one of 1,2 or 3: 2
Process finished with exit code 0
【问题讨论】:
-
你的代码会吐出一个错误
TypeError: unorderable types...,当你问一个包含错误和回溯的问题时,它只是不工作通常不是很丰富,甚至如果你转换你的代码会崩溃,如果用户输入一个不能转换为 int 的字符串,你应该使用 try/except
标签: python python-3.x user-input