2013-10-09 24 views
0
import random 
# program greeting 
print("The purpose of this exercise is to enter a number of coin values") 

print("that add up to a displayed target value.\n") 

print("Enter coins values as 1-penny, 5-nickel, 10-dime,and 25-quarter.") 

print("Hit return after the last entered coin value.") 

print("--------------------") 

#print("Enter coins that add up to 81 cents, one per line.") 

total = 0 

#prompt the user to start entering coin values that add up to 81 

final_coin= random.randint(1, 99) 

print ("Enter coins that add up to", final_coin, "cents, on per line") 

user_input = int(input("Enter first coin: ")) 
    total = total + user_input 


if user_input != 1 and user_input!=5 and user_input!=10 and user_input!=25: 
    print("invalid input") 

while total != final_coin: 
    user_input = int(input("Enter next coin: ")) 
    total = total + user_input 

if total > final_coin: 
    print("Sorry - total amount exceeds", (final_coin)) 

if total < final_coin: 
    print("Sorry - you only entered",(total)) 

if total== final_coin: 
    print("correct")  

如果用戶到達我希望他提示一個問題問他,如果他想再次嘗試我如何詢問用戶是否他要重複相同的任務再次

程序結束

回答

1

您可以將整個程序放在另一個while循環中,詢問用戶是否想再次嘗試。

while True: 
    # your entire program goes here 

    try_again = int(input("Press 1 to try again, 0 to exit. ")) 
    if try_again == 0: 
     break # break out of the outer while loop 
+0

你可能想要重命名你的變量,'continue'是一個關鍵字。 – FatalError

+0

@FatalError謝謝。我只是測試它,發現了同樣的錯誤。 :) –

相關問題