2016-09-22 25 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。