While loop does not recognise if statement and variables?
<p>I'm doing a code for a school project and I tried to include a while loop but the if statement that is inside this loop is not recognised and the program does not recognise the variable Correct_Weight under each statement and instead it takes it as 0 which causes a division by zero error. </p>

<p>the code is this:</p>

<pre><code>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 &lt;= 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 &gt;= 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") 
</code></pre>

<p>and the error is this:</p>

<p><a href="http://i.stack.imgur.com/mgciU.png" rel="nofollow"><img src="http://i.stack.imgur.com/mgciU.png" alt="enter image description here"></a></p>

<p>At the top, <code>Coin_Grams</code> is set to 0:</p>

<pre><code>Coin_Grams = 0
</code></pre>

<p>and you never set it to something else, because you <em>break out of the loop</em> immediately:</p>

<pre><code>while Continue == "y":
    Type_Of_Coin = input("Please enter the type of coin in the bag")
    break
</code></pre>

<p>It doesn't matter what other code is in the loop past that point, because you told Python to ignore it.</p>

<p>So you end up with <code>Coint_Grams</code> still set to <code>0</code> and that gives you a division by zero exception.</p>

<p>Put the <code>break</code> at the <em>end</em> of the loop, and use <code>continue</code> in the <code>else:</code> block:</p>

<pre><code>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
</code></pre>

<p>I also replaced the <code>while</code> condition; your <code>==</code> test is always true, and you don't need to change the value of the <code>Continue</code> variable, so you may as well just test for <code>while True</code> here.</p>

python loops while-loop project division