Cody Denisa - 7 months ago 21

Python Question

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.

the code is this:

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

and the error is this:

Answer

At the top, `Coin_Grams`

is set to 0:

```
Coin_Grams = 0
```

and you never set it to something else, because you *break out of the loop* immediately:

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

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

So you end up with `Coint_Grams`

still set to `0`

and that gives you a division by zero exception.

Put the `break`

at the *end* of the loop, and use `continue`

in the `else:`

block:

```
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
```

I also replaced the `while`

condition; your `==`

test is always true, and you don't need to change the value of the `Continue`

variable, so you may as well just test for `while True`

here.