Using a counter in a python function
Question:
I am trying to count the iterations in a function, but I can’t get it right. What is wrong with the code?
Every time I call the function, the counter give me zero as a result.
I want to add a pause between iteration an print the result one by one, but I couldn’t fix it.
My code is below:
n = 41
def collatz(n):
count = 0
if n != 1 and n % 2 == 1 :
n = n * 3 + 1
print(n)
count += 1
collatz(n)
elif n != 1 and n % 2 == 0:
n = n / 2
print(n)
count += 1
collatz(n)
else:
print('done')
print(count)
return
collatz(n)
Answers:
Its because in each recursion/function call, its again set to 0. You can pass it as an argument to the function to get past it.
Something like this should help solve it.
def collatz(n, count=0):
if n != 1 and n % 2 == 1 :
n = n * 3 + 1
print(n)
count += 1
collatz(n, count)
elif n != 1 and n % 2 == 0:
n = n / 2
print(n)
count += 1
collatz(n, count)
else:
print('done')
print(count)
return
collatz(n)
The other answer is correct, in that the issue is that the counter is reset at the beginning of each function call. The simplest alternative, which I do not recommend, is to use a global counter variable. The other answer (there’s only one at the moment) passes the iteration along in the recursive call, which also works.
Since it looks like you’re exploring the Collatz Conjecture, I expect that you’d like to do some more experiments with the sequence of numbers. To that end, it is better to make a generator for the sequence, then do whatever processing (counting and printing) you’d like on that sequence. This makes it much more versatile.
The following generator function can be used in the way you requested, but is more versatile.
def collatz(n):
while n>1:
if n%2 == 1:
n = 3*n + 1
else:
n = n//2
yield n
for n, count in enumerate(collatz(5)):
print(f'n={n+1},ttcount={count}')
#how many steps from 41
print(sum(1 for n in collatz(41)))
I am trying to count the iterations in a function, but I can’t get it right. What is wrong with the code?
Every time I call the function, the counter give me zero as a result.
I want to add a pause between iteration an print the result one by one, but I couldn’t fix it.
My code is below:
n = 41
def collatz(n):
count = 0
if n != 1 and n % 2 == 1 :
n = n * 3 + 1
print(n)
count += 1
collatz(n)
elif n != 1 and n % 2 == 0:
n = n / 2
print(n)
count += 1
collatz(n)
else:
print('done')
print(count)
return
collatz(n)
Its because in each recursion/function call, its again set to 0. You can pass it as an argument to the function to get past it.
Something like this should help solve it.
def collatz(n, count=0):
if n != 1 and n % 2 == 1 :
n = n * 3 + 1
print(n)
count += 1
collatz(n, count)
elif n != 1 and n % 2 == 0:
n = n / 2
print(n)
count += 1
collatz(n, count)
else:
print('done')
print(count)
return
collatz(n)
The other answer is correct, in that the issue is that the counter is reset at the beginning of each function call. The simplest alternative, which I do not recommend, is to use a global counter variable. The other answer (there’s only one at the moment) passes the iteration along in the recursive call, which also works.
Since it looks like you’re exploring the Collatz Conjecture, I expect that you’d like to do some more experiments with the sequence of numbers. To that end, it is better to make a generator for the sequence, then do whatever processing (counting and printing) you’d like on that sequence. This makes it much more versatile.
The following generator function can be used in the way you requested, but is more versatile.
def collatz(n):
while n>1:
if n%2 == 1:
n = 3*n + 1
else:
n = n//2
yield n
for n, count in enumerate(collatz(5)):
print(f'n={n+1},ttcount={count}')
#how many steps from 41
print(sum(1 for n in collatz(41)))