【发布时间】:2017-12-25 09:57:02
【问题描述】:
我想同时检查我的输入是否是数字并且在范围(1,3)内并重复输入直到我得到满意的答案。 现在我以这种方式执行此操作,但代码相当不干净和容易......有没有更好的方法来做到这一点? 也许有while循环?
def get_main_menu_choice():
ask = True
while ask:
try:
number = int(input('Chose an option from menu: '))
while number not in range(1, 3):
number = int(input('Please pick a number from the list: '))
ask = False
except: # just catch the exceptions you know!
print('Enter a number from the list')
return number
将不胜感激。
【问题讨论】:
标签: python python-3.x loops input