I need a counter variable k that needs to be defined outside the function where it is updated (this is because I want define a recursive function). I found out that I can only do this if k is an element of a list:
def g(k):
k +=1
return
def f():
k = 0
g(k)
return k
f()
0
vs.
def g(k):
k[0] +=1
return
def f():
k = [0]
g(k)
return k[0]
f()
1
Why is this? Is there a better way of doing this?
Below I show the real code:
def coins(n, m_25, m_10, m_5, m_1, m_tot, count):
if m_tot == n:
count[0] +=1
return
if m_tot > n:
return
coins(n, m_25+1, m_10, m_5, m_1, m_tot+25, count)
coins(n, m_25, m_10+1, m_5, m_1, m_tot+10, count)
coins(n, m_25, m_10, m_5+1, m_1, m_tot+5, count)
coins(n, m_25, m_10, m_5, m_1+1, m_tot+1, count)
def get_coins(n, m_25, m_10, m_5, m_1, m_tot):
count = [0]
coins(n, m_25, m_10, m_5, m_1, m_tot, count)
return count[0]