while循环不识别if语句和变量?

While loop does not recognise if statement and variables?

我正在为一个学校项目编写代码,我试图包含一个 while 循环,但无法识别此循环内的 if 语句,并且程序无法识别每个下的变量 Correct_Weight声明,而是将其视为 0,这会导致被零除错误。

代码是这样的:

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

错误是这样的:

在顶部,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放在循环的[​​=35=]结尾,并在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