我正在嘗試進行一項允許用戶從選項1-x中選擇的多項選擇調查。我怎樣才能讓這個如果用戶輸入任何字符,除了數字,返回類似「這是一個無效的答覆」除其他我如何限制用戶輸入爲Python中的整數
def Survey():
print('1) Blue')
print('2) Red')
print('3) Yellow')
question = int(input('Out of these options\(1,2,3), which is your favourite?'))
if question == 1:
print('Nice!')
elif question == 2:
print('Cool')
elif question == 3:
print('Awesome!')
else:
print('That\'s not an option!')
捕獲異常 –