def sequence(n):
while n!=1:
print(n)
if n%2==0:
sequence(n/2)
else:
sequence(n*3+1)
sequence(53)
This loop is not terminating. I don't understand why.
Remove the while loop. You don't need that as the recursive function is doing its job.
Here is the corrected version of your code:
def sequence(n):
print(n)
if n == 1:
return
elif n%2==0:
sequence(n//2)
else:
sequence(n*3+1)
sequence(53)
Change the n/2 to n//2 so that it doesn't become a float.
ninside your function so thewhileloop never terminates. Perhaps you meantif n != 1:?whileloop and recursion.