0

I'm trying to write a collatz program from the 'Automate the Boring Stuff with Python book', but have ran into some problems. I'm using python 3.5.2. Here's the project outline:

Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is odd, then collatz() should print and return 3 * number + 1. Then write a program that lets the user type in an integer and that keeps calling collatz() on that number until the function returns the value 1.

My code:

def collatz(number):
    if number % 2 == 0:     #its even
        print(number // 2)
        return number // 2
    elif number % 2 == 1:   #its odd
        print(3*number+1)
        return 3*number+1


print('Type an integer: ')
num=int(input())

while(True):
   if collatz(num) == 1:
        break

 # Or even simpler:
 # while(collatz(num) != 1):
 #       pass

The output gives me an infinite loop:

Type an integer: 
10
5
5
5
5
5
5
5
5
...

But when I break it down and use a variable to store the return value, it works:

 while(True):
    num=collatz(num)
    if num == 1:
        break

Output:

Type an integer: 
5
16
8
4
2
1

Why is it? I don't understand why the first program doesn't work. Both are similar but I just chose to test the return value directly in my original program instead of using variables. I'd appreciate any help, Thanks.

2
  • 6
    Your infinite loop version never updates the value of num. Commented Sep 29, 2016 at 22:32
  • 1
    Well that was a very dumb mistake from my part. Well it happens.. Thanks man. Commented Sep 29, 2016 at 22:37

1 Answer 1

2

Your code:

while(True):
    if collatz(num) == 1:
        break

didn't work because everytime collatz gets called it gets called with the same value of num and as a result returns the same number again and again. That number is not 1, so you have an infinite loop.

When you do num = collatz(num), the value of num is changed the first time the function is called. The new value is then passed to the second time the function is called, and so on. So eventually you reach a point when the value of num becomes 1 and exit the loop.

Sign up to request clarification or add additional context in comments.

3 Comments

Please consider not answering questions that boil down to typo-grade issues and can be answered in a comment. These questions are not helpful for future readers, but providing full answers can hinder automatic deletion precesses, as well as cluttering up Stack Overflow in the long run.
@AndrasDeak I'm not aware of how we define typo-grade issues here. Can you point me to some guidelines? From what I see the OP has problem understanding the logic of his code. For experienced programmers this is trivial but for beginners it may not be the same. The mistake he made is not a typo but a bug in the code.
You're right, I tend to consider typos the same category as very basic mistakes that have a hundred duplicates:) Here's the text from the close reason corresponding to typo-like problems: "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. [...]". I meant my comment in the spirit of the second sentence. I do believe your answer is good, and you can keep it around. Just wanted to consider this aspect in the future:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.