2016-09-22 63 views
1

我已经在下面编写了Python代码(实际上它是我在第24页的“在24小时内自学Python”练习的解决方案)。如何在循环内发出警告并要求输入raw_input

想法是:餐桌周围有4个座位,服务员知道每个座位的订购量,输入这4个数量并获得总数。

如果提供的raw_input不是一个数字(而是一个字符串),我的代码会将这个人踢出去。但是,目标是给出错误消息(“此条目无效”)并再次询问输入 - 直到它为数字。但是,我无法弄清楚如何再次询问用户的原始输入 - 因为我已经在循环中了。

非常感谢您的建议!

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!" 

回答

2

一般情况下,当你不知道要多少次运行循环的解决方案是一个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) 
+0

非常感谢 - 这个解决方案非常棒! – user3245256

0
total = 0 
for seat in range(1,5): 
    incorrectInput = True 
    while(incorrectInput): 
     print 'Note the amount for seat', seat, 'and' 
     myinput = raw_input("enter it here ['q' to quit]: ") 

     if myinput == 'q': 
      print 'Goodbye' 
      quit() 

     elif is_numeric(myinput): 
      floatinput = float(myinput) 
      total = total + floatinput 
      incorrectInput = False 

     else: 
      print 'I\'m sorry, but {} isn\'t valid. Please try again'.format(myinput) 


total = round(total, 2) 
print "*****\nTotal: ${}".format(total) 
print "Goodbye!" 
+0

这(第二)解决方案不完全正常工作(顺便说一句,一个错字,而不是错误)。如果它是以这种方式构建的,那么当我输入错误的数量(字符串)时,它仍然移动到第二个席位,并将前一个席位的数量设为0. – user3245256

+0

对不起,我在else语句中留下了休息时间。对我来说,总是用解释器测试代码是一个很好的教训。 – SilentLupin

0

帕特里克霍先生和SilentLupin建议,一个while循环可能是最好的办法。另一种方式是recursion-即遍地调用同一个功能,直到你得到一个有效的输入:

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_valid_input电话本身一遍又一遍,直到供应validators之一产生什么truthy。