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

时间:2016-09-17 16:17:46

标签: python loops while-loop project division

我正在为一个学校项目做一个代码,我尝试包含一个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") 

,错误是这样的:

enter image description here

1 个答案:

答案 0 :(得分: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放在循环的 end 上,并使用continue块中的else:

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