## Can't figure out how to increment a variable within a recursive function

can definition
can synonym
can verb
can noun meaning
can bus
can band
cant
can't

I am trying to create a python script for the collatz conjecture (if a number's odd, times it by three and add 1, and if it's even, divide by two). I can't figure out how to successfully increment a variable that shows how many iterations of the function and I think it has something to do with it being recursive. Here's what I've got so far:

```def collatz(n):
list_terminate = [0, 1, 2, 4]
if n in list_terminate:
return n
else:
if n % 2 == 0:
print(n)
iterations += 1
return collatz(n // 2)
if n % 2 == 1:
iterations += 1
print(n)
return collatz((n * 3) + 1)
```

The variable does not persist between function calls. If you want to have multiple values passed between functions, then pass it as a parameter to your function and return both.

```def collatz(n, iterations):
list_terminate = [0, 1, 2, 4]
if n in list_terminate:
return (n, iterations)
else:
if n % 2 == 0:
print(n)
iterations += 1
return collatz(n // 2, iterations)
if n % 2 == 1:
iterations += 1
print(n)
return collatz((n * 3) + 1, iterations)
```

If you don't like calling your function like, `collatz(n, 0)`, then use this is a wrapper function.

```def collatz_helper(n, iterations):
list_terminate = [0, 1, 2, 4]
if n in list_terminate:
return (n, iterations)
else:
if n % 2 == 0:
print(n)
iterations += 1
return collatz(n // 2, iterations)
if n % 2 == 1:
iterations += 1
print(n)
return collatz((n * 3) + 1, iterations)

def collatz(n):
return collatz_helper(n, 0)
```

Can, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers. Aluminum can � Drink can � Oil can � Steel and tin cans � Trash can� Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

Your problem statement is little confusing but you may try the below code, if it suits you.

```def collatz(n):
iterations = 0
list_terminate = [0, 1, 2, 4]
if n in list_terminate:
return n
elif n % 2 == 0:
# print(n)
iterations += 1
return ("No. of iterations : ", iterations, n//2)
elif n % 2 == 1:
iterations += 1
# print(n)
return ("No. of iterations : ", iterations,(n * 3) + 1)
```

Can, Can was the leading avant-garde rock group of the 70s. Can experimented with noise, synthesizers, non-traditional music, cut-and-paste techniques, and, most� Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

There are a few options...

1) Change the function signature to include the number of iterations. This way, you can pass the information 'up the chain'

2) Change the scope of num iterations such that the variable exists outside the function.

3) Add a recursive function inside collatz.

approach (1, 3)

```def collatz(n):

def recursion(x, iterations):
print(f"{iterations}: {x}")
if x in [0,1,2,4]:
return x, iterations

if x % 2 == 0:
return recursion(x//2, iterations + 1)
else:
return recursion((x*3) + 1, iterations + 1)

return n

n, steps = recursion(n, 0)
```

approach (2)

```iterations = 0
def collatz(n):
global iterations

print(f"{iterations}: {n}")
if n in [0,1,2,4]:
return n, iterations

iterations += 1
if n % 2 == 0:
return collatz(n//2)
else:
return collatz((n*3) + 1)
```

The Official CAN / Spoon Records Website, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). can definition: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more.

can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

Can, can noun [C] (CONTAINER) � can verb [T] (CONTAIN) � can modal verb (ABLE) � can modal verb (PERMIT) � can modal verb (BE POSSIBLE) � can� Etymology 1 From Middle English can, first and third person singular of connen, cunnen (“to be able, know how”), from Old English can (n), first and third person singular of cunnan (“to know how”), from Proto-Germanic *kunnaną, from Proto-Indo-European *ǵneh₃- (whence know).

CAN, Word forms � 1. countable noun A can is a metal container in which something such as food, drink, or paint is put. � 2. verb [usually passive] When food or drink is� HANGZHOU, China, Aug. 04, 2020 (GLOBE NEWSWIRE) -- Canaan Inc. (NASDAQ: CAN) ("Canaan" or the "Company"), a leading high-performance computing solutions provider, today announced that the terms of

##### Comments
• Sorry it’s my first time using stack overflow. Thanks for your help
• So helpful. Thank you!