Sep-30-2019, 12:23 PM
I am trying to answer one of the questions in Automate the Boring Stuff with Python. The function (which seems to work correctly) should retun x if an even number and 3*x+1 if an odd number. I am then trying to call the function with a user given integer, calling the function until it gives the answer 1 and then ending the process. When I run it I keep getting the same number (which I think is the return value of the first function call) repeated indefinitely. Can you please tell me where I am going wrong, I suspect it is something to do with scoping rules, but don't know what.
1 2 3 4 5 6 7 8 9 10 11 |
def collatz(x): if x % 2 = = 0 : return x else : return x * 3 + 1 y = int ( input ( "Enter a number\n" )) while True : y = collatz(y) print (y) if y = = 1 : break |