In the below code segment I am trying to set a global variable called smallest to store the smallest number of coins which can form the amount. I am getting an error in the helper function while comparing num < smallest. It seems like it is accessing a local variable while I want to access a global variable.
cannot access local variable ‘smallest’ where it is not associated with a value
smallest = 98
def main():
coins = [186,419,83,408]
amount = 6249
coinChange(coins, amount)
print(smallest)
def coinChange(coins, amount):
helper(coins, amount, 0)
def helper(coins, amount, num):
if amount == 0:
if num < smallest:
smallest = num
return
if amount < 0:
return
for coin in coins:
helper(coins, amount - coin, num + 1)
if __name__ == '__main__':
main()```