2016-09-17 73 views
-1

我正在做一個學校項目的代碼,我試圖包括一個while循環,但是在這個循環內部的if語句無法識別,程序在每個語句下都無法識別變量Correct_Weight,而是將其作爲0導致零誤差除法。while循環無法識別語句和變量?

的代碼是這樣的:

Coin_Grams = 0 
Correct_Weight = 0 
Difference = 0 
Total_Coins_Removed = [] 
Total_Coins_Added = [] 
Coins_To_Remove = 0 
Coins_To_Add = 0 
Number_Of_Bags_Checked = 0 
Continue = "y" 

print ("Welcome to our program!") 

while Continue == "y": 
    Type_Of_Coin = input("Please enter the type of coin in the bag") 
    break 
    if Type_Of_Coin == "1 pence": 
     Coin_Grams = 3.56 
     Correct_Weight = Coin_Grams * 100 
    elif Type_Of_Coin == "2 pence": 
     Coin_Grams = 7.12 
     Correct_Weight = Coin_Grams * 50 
    elif Type_Of_Coin == "5 pence": 
     Coin_Grams = 3.25 
     Correct_Weight = Coin_Grams * 100 
    elif Type_Of_Coin == "10 pence": 
     Coin_Grams = 6.50 
     Correct_Weight = Coin_Grams * 50 
    elif Type_Of_Coin == "20 pence": 
     Coin_Grams = 5.00 
     Correct_Weight = Coin_Grams * 50 
    elif Type_Of_Coin == "50 pence": 
     Coin_Grams = 8.00 
     Correct_Weight = Coin_Grams * 20 
    elif Type_Of_Coin == "1 pound": 
     Coin_Grams = 9.50 
     Correct_Weight = Coin_Grams * 20 
    elif Type_Of_Coin == "2 pounds": 
     Coin_Grams = 12.00 
     Correct_Weight = Coin_Grams * 10 
    else: 
     print ("Type of coin is wrong please try again") 


Current_Weight = int(input("How much does the bag weight?")) 

Difference = Current_Weight - Correct_Weight 
print ("The difference is" ,Difference, "grams") 

if Difference <= 0: 
    Coins_To_Add = abs(Difference)/Coin_Grams 
    Total_Coins_Add.append(Coins_To_Add) 
    print ("You need to add" ,round(Coins_To_Add), "coins") 
elif Difference >= 0: 
    Coins_To_Remove = Difference/Coin_Grams 
    Total_Coins_Removed.append(Coins_To_Remove) 
    print ("You need to remove" ,round(Coins_To_Remove), "coins") 
else: 
    print ("You don't need to remove or add any coins") 
Number_Of_Bags_Checked = Number_Of_Bags_Checked + 1 

Continue = input("Do you have any more bags to check? please answer as y or n") 
print ("\n") 

if Continue == "n": 
    print("\n") 
    print (Number_Of_Bags_Checked,"bags have been checked") 
    print ("\n") 
    print (Total_Coins_Removed,"coins have been removed in total") 
    print ("\n") 
    print (Total_Coins_Added,"coins have been added in total") 

和錯誤是這樣的:

enter image description here

+0

什麼是無條件的'break'在你的while循環中做什麼? – Mat

+0

你可以在循環的頂部執行'break'。 –

+1

請將錯誤消息*作爲文本*置於您的問題中。屏幕截圖無法搜索。 –

回答

0

在頂部,Coin_Grams設置爲0:

Coin_Grams = 0 

和你從來沒有把它設置爲別的,因爲你打出來馬上循環的:

while Continue == "y": 
    Type_Of_Coin = input("Please enter the type of coin in the bag") 
    break 

不要緊,在循環中的該點什麼其他的代碼,因爲你告訴Python來忽略它。

所以你最終Coint_Grams仍然設置爲0,這給你一個零除異常的除法。

break在循環的,並在else:塊使用continue

while True: 
    Type_Of_Coin = input("Please enter the type of coin in the bag") 
    if Type_Of_Coin == "1 pence": 
     # etc. 

    else: 
     print ("Type of coin is wrong please try again") 
     # when you get here the type of coin was incorrect, start again 
     continue 

    break # when you get here the type of coin was correct, so break out 

我也換成了while條件;您的==測試總是如此,您不需要更改Continue變量的值,因此您可以在這裏測試while True

+0

okei所以,如果我刪除break,那麼Type_Of_Coin輸入問題會不斷重複。 –

+0

@CodyDenisa:我早就加入瞭如何解決這個問題;將'break'移動到* end *並確保在硬幣類型輸入錯誤時通過向'else'塊添加'continue'來確保它沒有到達。 –

+0

@MartijnPieters好的,我已經完成了你向我展示的內容,它運行良好,它告訴我不同​​之處,有多少是需要刪除或添加的,但是當我回答我想繼續繼續問題時,什麼也沒有發生,任何想法爲什麼?因爲如果用戶想要檢查另一個包,我希望程序再次重複,while循環應該再次用Type_Of_Coin問題 –