0

Why this While loop is not breaking or stopping

I have added some output screenshot

term = 0
i = 13
while True:
    print i > 1
    print "i = ",i
    if i == 1:
        term += 1
        break
    if i%2 == 0:
        i = i / 2
        term += 1
    if i%2 != 0:
        i = i * 3 + 1
        term += 1

Output

I also tried This way too

term = 1
i = 13
while i > 1:
    print i > 1
    if i%2 == 0:
        i = i / 2
        term += 1
    if i%2 != 0:
        i = i * 3 + 1
        term += 1
8
  • 1
    It's not breaking or stopping because i oscillates between 2 and 4 infinitely based on how you've encoded the math for the next i so the stopping condition is unreachable. What is it supposed to do? Is this supposed to run Collatz? Commented Jul 9, 2020 at 18:11
  • mama, see the first one I use break in first if block Commented Jul 9, 2020 at 18:12
  • 1
    @mama What do you mean? There is a break statement in the first one, and the second has a loop condition. Commented Jul 9, 2020 at 18:12
  • 1
    @rid This program is testing the Collatz conjecture and if coded correctly will eventually reach 1. Commented Jul 9, 2020 at 18:15
  • 4
    You appear to be using Python 2 (based on the use of a print statement). As a beginner, you should be learning Python 3 instead. Commented Jul 9, 2020 at 18:15

3 Answers 3

4

Use elif to make the cases mutually exclusive. You don't want multiple if statements to execute in the same loop iteration.

if i%2 == 0:
    i = i / 2
    term += 1
elif i%2 != 0:
    i = i * 3 + 1
    term += 1

Or just make it else since the second condition is redundant.

if i%2 == 0:
    i = i / 2
    term += 1
else:
    i = i * 3 + 1
    term += 1

The reason it oscillates between 2 and 4 as written is because 2 causes both if statements to run. 2 is even so the first one runs and halves i, making it 1. Now it's odd and the second one triggers, turning 1 into 4.

if i%2 == 0:
    i = i / 2       # 2 --> 1
    term += 1
if i%2 != 0:
    i = i * 3 + 1   # 1 --> 4
    term += 1

The next iteration 4 becomes 2.

if i%2 == 0:
    i = i / 2       # 4 --> 2
    term += 1

These two iterations repeat over and over in an endless cycle.

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

Comments

1

Let's say your i is 2. It is divisible by 2, so if i % 2 == 0 fires, and i becomes 1. And the code continues to execute, so now we are at if i % 2 != 0 line, and this condition is also true, because you just modified i and it's now 1. So i becomes 4.

Your modified second attempt, which prevents the second condition from being checked if the first one succeeds, is below:

term = 1
i = 13
while i > 1:
    print(i > 1)
    if i % 2 == 0:
        i = i / 2
        term += 1
    elif i % 2 != 0:
        i = i * 3 + 1
        term += 1

Also notice that you actually don't need to check the second condition, as it is definitely true if the first one is not, so elif ... line can be replaced just with else:

You can also use continue keyword to stop the rest of the loop from executing if the first condition is true:

term = 1
i = 13
while i > 1:
    print(i > 1)
    if i % 2 == 0:
        i = i / 2
        term += 1
        continue
    if i % 2 != 0:
        i = i * 3 + 1
        term += 1

Your first attempt has exactly the same problem; fixing it I leave as an exercise for the reader :)

P.S. do not learn Python 2

1 Comment

0

The problem is:

if i%2 == 0:
    i = i / 2
    term += 1
if i%2 != 0:
    i = i * 3 + 1
    term += 1

The problem is that, if i % 2 == 0 is true, it will remain true until i = 1. Once i = 1, i % 2 != 0 executes and makes i = 4.

Comments

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.