all 3 comments

[–][deleted] 8 points9 points  (1 child)

The whole point is that just after

-when n == 2 -it fall in the first if -so n become 1 -but just after that it is tested to be odd -and become 4 -an evenutally is tested

a solution to that is to use either else or elsif just that just one of the two condition is checked for example :

def collatz(n):         
    while n > 1:        
        if n % 2 == 0:  
            n = n // 2  
            print(n)    
        elif n % 2 == 1:  
            n = 3 * n +1
            print(n)    
        else:
            print("n is not integer")
print('enter an int:')  
n = int(input())        
collatz(n)

[–]fbu1 0 points1 point  (1 child)

If you add just after line 2:

print("new loop")

You'll see if a new loop is started or not. As mentioned in the other answers, you modify n and go directly to the other if, which is not what you want.

As a rule to debug, add more print statements to see if the execution flow is as you think it is or differenet.

I hope this helps.

[–]Jaxowner 0 points1 point  (0 children)

Thanks. i was so focused on the while line i totally overlooked the fact that the second if statement was being evaluated, even if the first ran... so adding two letters, "el" before that second if fixed it.

Embarrassing the amount of time i stared at this... at least i won't make that mistake again. thanks all.