python 做一个循环 要求输入大于0的数字 判断其不

2024-12-01 19:28:19
推荐回答(1个)
回答1:

def getInt(prompt, limit=(0, None)):
while True:
try:
x = int(input(prompt))
if limit[0] is not None and x < limit[0]:
continue
if limit[1] is not None and limit[1] < x:
continue
return x
except:
pass

def setlimits():
lb = getInt('Please enter a Low bound: ', (1, None))
hb = getInt('Please enter a High bound: ', (lb, 9999))
return (lb, hb)

lb, hb = setlimits()
num = getInt('Please enter a number between %d and %d' % (lb, hb),
limit=(lb, hb))