Python 如何在循环中发出警告并再次请求原始输入
我已经在下面编写了Python代码(实际上,这是我在“24小时内自学Python”第80页练习中的解决方案) 这个想法是:桌子周围有4个座位,服务员知道每个座位要多少,输入这4个数量,得到总数 如果提供的原始输入不是一个数字(而是一个字符串),我的代码会将此人踢出。然而,目标是给出一条错误消息(“此条目无效”)并再次请求输入,直到输入为数字。但是,我不知道如何再次向用户请求原始输入,因为我已经在一个循环中了 非常感谢你的建议Python 如何在循环中发出警告并再次请求原始输入,python,Python,我已经在下面编写了Python代码(实际上,这是我在“24小时内自学Python”第80页练习中的解决方案) 这个想法是:桌子周围有4个座位,服务员知道每个座位要多少,输入这4个数量,得到总数 如果提供的原始输入不是一个数字(而是一个字符串),我的代码会将此人踢出。然而,目标是给出一条错误消息(“此条目无效”)并再次请求输入,直到输入为数字。但是,我不知道如何再次向用户请求原始输入,因为我已经在一个循环中了 非常感谢你的建议 def is_numeric(value): try: i
def is_numeric(value):
try:
input = float(value)
except ValueError:
return False
else:
return True
total = 0
for seat in range(1,5):
print 'Note the amount for seat', seat, 'and'
myinput = raw_input("enter it here ['q' to quit]: ")
if myinput == 'q':
break
elif is_numeric(myinput):
floatinput = float(myinput)
total = total + floatinput
else:
print 'I\'m sorry, but {} isn\'t valid. Please try again'.format(myinput)
break
if myinput == 'q':
print "Goodbye!"
else:
total = round(total, 2)
print "*****\nTotal: ${}".format(total)
print "Goodbye!"
通常,当您不知道要运行循环多少次时,解决方案是
while
循环
for seat in range(1,5):
my_input = raw_input("Enter: ")
while not(my_input == 'q' or isnumeric(my_input)):
my_input = raw_imput("Please re-enter value")
if my_input == 'q':
break
else:
total += float(my_input)
正如Patrick Haugh和SilentLupin所建议的那样,
while
循环可能是最好的方法。另一种方法是递归-即反复调用同一函数,直到得到有效输入:
def is_numeric(value):
try:
input = float(value)
except ValueError:
return False
else:
return True
def is_q(value):
return value == 'q'
def is_valid(value, validators):
return any(validator(input) for validator in validators)
def get_valid_input(msg, validators):
value = raw_input(msg)
if not is_valid(value, validators):
print 'I\'m sorry, but {} isn\'t valid. Please try again'.format(value)
value = get_valid_input(msg, validators)
return value
total = 0
for seat in range(1,5):
print 'Note the amount for seat', seat, 'and'
myinput = get_valid_input("enter it here ['q' to quit]: ", [is_q, is_numeric])
if myinput == 'q':
break
elif is_numeric(myinput):
floatinput = float(myinput)
total = total + floatinput
if myinput == 'q':
print "Goodbye!"
else:
total = round(total, 2)
print "*****\nTotal: ${}".format(total)
print "Goodbye!"
在上面的代码中,
get\u valid\u input
反复调用自身,直到提供的验证程序之一产生真实的结果 这个(第二个)解决方案并不完全有效(顺便说一句,一个输入错误,不是错误)。如果它是这样构建的,那么当我输入错误的金额(字符串)时,它仍然会移到第二个席位,并将前一个席位的金额视为0。对不起,我在else语句中留下了中断符。对我来说,这是一个很好的教训,让我总是用解释器测试代码。
def is_numeric(value):
try:
input = float(value)
except ValueError:
return False
else:
return True
def is_q(value):
return value == 'q'
def is_valid(value, validators):
return any(validator(input) for validator in validators)
def get_valid_input(msg, validators):
value = raw_input(msg)
if not is_valid(value, validators):
print 'I\'m sorry, but {} isn\'t valid. Please try again'.format(value)
value = get_valid_input(msg, validators)
return value
total = 0
for seat in range(1,5):
print 'Note the amount for seat', seat, 'and'
myinput = get_valid_input("enter it here ['q' to quit]: ", [is_q, is_numeric])
if myinput == 'q':
break
elif is_numeric(myinput):
floatinput = float(myinput)
total = total + floatinput
if myinput == 'q':
print "Goodbye!"
else:
total = round(total, 2)
print "*****\nTotal: ${}".format(total)
print "Goodbye!"