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

如题所述

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))
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答