0

I have recently started studying Python and the book tasked me with creating a function that would write the Collatz sequence starting with an input number but, despite the while loop, it will only run once if i use the return statement. Why is that? The program runs correctly if i change it into n =, but what's the point of return then?

Here's the current code:

def collatz(n):
    while n!=1:
        print(n) 
        if n%2==0:            
            return n//2   
        else: 
            return n*3+1

print('Insert a number.')
while True:
    try:
        print(collatz(int(input())))
    except ValueError:
        print('Please insert an integer number.')

If i were to type 33, it will only print:

>>>>33
>>>>100

and then stop. Thus returning the new value only once.

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
Buddhino
  • 11
  • 1
  • 1
    You want to write `n = n // 2` and `n = n * 3 + 1`. `return` says to return from the function immediately, and that's what your code is doing. – Frank Yellin Mar 14 '23 at 17:45
  • 1
    `return` terminates the function. So your `while` loop will only run once. It feels like you intended to **reassign** n to that new value, instead of **returning** it... – John Gordon Mar 14 '23 at 17:46
  • The point of `return` is to do something you don't want to do with this function. *These particular* return statements have not point, and indeed are just incorrectly used. – chepner Mar 14 '23 at 17:53
  • Suggestion. Use pythontutor to step through line by line and visualize the workings. [link](https://pythontutor.com/python-debugger.html#mode=edit). You can simply copy and paste your code. – user19077881 Mar 14 '23 at 17:53

2 Answers2

0

What you're looking for is probably this:

def collatz(n):
    while n != 1:
        print(n)
        if n % 2 == 0:
            n = n // 2
        else:
            n = n * 3 + 1
    return n

The "point" of return is to provide output from the function. For example, x = collatz(1) will make print(x) say 1.

Just a fun fact, you can make a function that returns more than once with the yield keyword. I don't think that's what you're looking for as far as solving this, but you can in fact make multiple returns before the function terminates by using a for-loop over the function call when using yield instead of return.

Brock Brown
  • 956
  • 5
  • 11
0

It seems like you are running into the same issue found here Which I think it's just an error in the book. It looks like it has you put the while loop inside the function, but then as soon as you return, you don't repeat even if the number isn't equal to 1 because you exit the while loop and the function. The highest voted answer on that question has it a better way where you move the while loop outside of the function, so the you will re-run the function if the number is not equal to 1.

If you didn't want to have the while loop out of the function you could do:

def collatz(number):

    while number != 1:
        if number % 2 == 0:
            print(number // 2)
            number = number // 2

        elif number % 2 == 1:
            result = 3 * number + 1
            print(result)
            number = result

    print("This should be 1: ", number)
    return number
Keenan
  • 399
  • 3
  • 6