您的代码将变为:
def Survey():
print('1) Blue')
print('2) Red')
print('3) Yellow')
while True:
try:
question = int(input('Out of these options\(1,2,3), which is your favourite?'))
break
except:
print("That's not a valid option!")
if question == 1:
print('Nice!')
elif question == 2:
print('Cool')
elif question == 3:
print('Awesome!')
else:
print('That\'s not an option!')
它的工作方式是使循环无限循环,直到仅放入数字为止。因此,如果我输入“ 1”,它将破坏循环。但是如果我放“ Fooey!” 该except
语句将捕获“将引发的错误” ,并且该循环将循环,因为它没有被破坏。