1

I want the loop to stop when one of the numbers reaches 4 million. But it's working incorrectly. Could someone help me?

number1=0
number2=1
while number1<(4000000):
 number1+=number2
 number2+=number1
 print(number1,number2)

Here are the numbers I get:

1 2
3 5
8 13
21 34
55 89
144 233
377 610
987 1597
2584 4181
6765 10946
17711 28657
46368 75025
121393 196418
317811 514229
832040 1346269
2178309 3524578
5702887 9227465
user14678216
3,4802 gold badges23 silver badges50 bronze badges
asked Nov 28, 2020 at 15:05
5
  • Why do you think it's working incorrectly? Have you debugged this program? Have you checked the values of number1? Commented Nov 28, 2020 at 15:08
  • Yes, I provided the numbers I get: they are more than 4000000 and the program does not stop Commented Nov 28, 2020 at 15:12
  • At the last iteration of the loop, number1 is at value: 2 178 309 and number2 is at: 3 524 578. These values are less than 4 000 000. Plus, you are only asking the value named nunber1 to be less than 4 million. Commented Nov 28, 2020 at 15:14
  • 1
    This is because the condition of the loop is just evaluated before the loop is repeated, but that is after the values are printed. It's not that the loop condition is guaranteed to be kept in every place in the loop body, but just at the start of the loop. So you just need to change your code, that the printing comes before the changeing of the values, or you wrap an if statement around the print code. Commented Nov 28, 2020 at 15:17
  • I voted you up because you are new... but next time try to give more information and Expected output at least.. Commented Nov 28, 2020 at 15:37

3 Answers 3

4

It is working correctly - when number1 reaches 4 million it stops. If the issue is that numbers above 4 million are printed you could break an infinite loop instead:

while True:
 number1+=number2
 number2+=number1
 if number1 > 4000000:
 break
 print(number1, number2)
answered Nov 28, 2020 at 15:12
Sign up to request clarification or add additional context in comments.

Comments

0

Be a pro about it. No need to restructure your code. Just skip the last iteration. This basically what we want to do.

number1=0
number2=1
while number1<(4000000):
 print(number1,number2)
 number1+=number2
 number2+=number1
answered Nov 28, 2020 at 15:18

2 Comments

this prints 0 1 at first which the OP does not. Not very pro :)
You are absolutely correct. This is not what we want, the goal of keeping the code structured like this will either mess up the first or the last iteration.
0

There are a lot of options to do it. The main problem is the logical sequence:

check -> increment -> print

Whenever you increment after check, the printed value is greater than the value used for comparison.

And another option in addition (not the best one):

number1 = 0
number2 = 1
while True:
 number1 += number2
 number2 += number1
 if number1 <= (4000000):
 print(number1, number2)
 else:
 break

You should use <= instead of < for a case when number1 exactly equals 4000000. With <, it will go to infinity loop.

answered Nov 28, 2020 at 15:25

1 Comment

Mimicing do ... while! Better approach :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.