I am trying to add a python function to my code, but when I do so I get an UnboundLocalError:
Traceback (most recent call last):
File "/Users/name/Documents/project.py", line 44, in logic(coinType, 3.56, bagWeight, 356, 0.01)
File "/Users/name/Documents/project.py", line 14, in logic valueAddedCoins += value UnboundLocalError: local variable 'valueAddedCoins' referenced before assignment
def logic(coin_txt, w1, wBag, cWeight, vCoin):
diff = abs(wBag - cWeight)
if diff == 0:
print("Bag ok")
return
coins = diff / w1
value = coins * vCoin
if wBag < cWeight:
valueAddedCoins += value
print(int(coins), coin_txt, " coins missing")
print(diff, "grams too little")
else:
valueRemovedCoins += value
print(int(coins), coin_txt, " coins too many")
print(diff, " grams too many")
valueAddedCoins = 0
valueRemovedCoins = 0
numBagsChecked = 0
continueChecking = True;
while continueChecking:
#asking information about the coins and deducing wether or not the weight is correct
bagWeight = float(input("Weight of bag of coins (no unit): "))
coinType = input("Type of coins in bag: 1 pence or 2 pence?")
numBagsChecked += 1
if coinType == "1 pence":
logic(coinType, 3.56, bagWeight, 356, 0.01)
elif type_of_coin == "2 pence":
logic(coinType, 7.12, bagWeight, 712, 0.02)
check = input("Another bag? (Y/N): ")
if check == "N":
continueChecking = False
Why am I getting an UnboundLocalError?