To start, I'm a beginner when it comes to working with python. I'm writing a function for the coin-change problem, with additional constraints (find the best way to pay given a list of the amount of each coin type and their value).
The function was working fine and I was almost done when suddenly statements at the start of my function were causing syntax errors. I hadn't been editing these at all as they'd worked, I was sorting out the last few kinks when it happened.
I've pasted the start of my function, note change(amount) will return the most appropriate way to pay amount. Can anyone suggest why this would start presenting a syntax error when it hadn't done so on many previous tests?
def pay_with_coins(amount, pocket):
change_otpt = change(amount)
import copy
amount = amount*100 #puts amount in pennies
original_pocket = copy.deepcopy(pocket)
original_amount = copy.deepcopy(amount)
change_amount = copy.deepcopy(amount)
original_pocket2 = copy.deepcopy(original_pocket)
original_amount2 = (copy.deepcopy(original_amount)
coins = [200,100,50,20,10,5,2,1] #syntax error 1
output = [0 for i in range(9)] #syntax error 2
##find pocket value
def value(pocket):
value = 0
for i in range(8):
value += (pocket[i]*coins[i])
return value
pocket_val = value(pocket)
#print("pocket value is:", pocket_val)
##easy case: is there enough money to pay (pocket value less than)
if pocket_val < amount:
print('False')
return False
##checking if change() can give a suitable answer########
count = 0 #variable to check how indexes in change_otpt are within pocket
for i in range (8):
if change_otpt[i] <= pocket[i]:
count += 1
if count == 8:#i.e output of change() is contained within pocket
change_otpt.append(0)
elif:
change_otpt = [0 for i in range(8)]
change_used = sum(change_otpt) #how many coins did this method use?
print('change method returned: ', change_otpt)
for i in range(7):
change_amount -= change_otpt[i]*coins[i]
print('change_amount is',change_amount)