我正在做一個學校項目的代碼,我試圖包括一個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")
和錯誤是這樣的:
什麼是無條件的'break'在你的while循環中做什麼? – Mat
你可以在循環的頂部執行'break'。 –
請將錯誤消息*作爲文本*置於您的問題中。屏幕截圖無法搜索。 –