The Collatz conjecture is that you will reach one, not zero; when you reach one, you should stop. Also, you have an odd combination of while
loop and recursive calling. A very simple recursive implementation:
def collatz(n):
print(n)
if n == 1: # base case
print("Done!")
else:
if n % 2: # odd number
collatz((3 * n) + 1)
else: # even number
collatz(n / 2)
or iterative version:
def collatz(n):
while n != 1:
print(n)
if n % 2: # odd number
n = (3 * n) + 1
else: # even number
n /= 2
print(n)
print("Done!")
If you want to analyse how long a number takes to reach one, you can rejig one of those implementations, e.g.:
def collatz(n):
count = 0
while n != 1:
count += 1
if n % 2: # odd number
n = (3 * n) + 1
else: # even number
n /= 2
return count
You can then call this function, working through the integers, creating the Collatz sequence for each one, for example:
seq_len = [(n, collatz(n)) for n in range(1, 101)]