-1

These were my instructions:

Write a program using while loop, which prints the sum of every third numbers from 1 to 1001 ( both 1 and 1001 are included)

(1 + 4 + 7 + 10 + ....)

Here is my code:

    num = 0
    x = 1
    while x != 1001:
        num += x
        x += 3

    print(num)

Can someone point out where I've gone wrong?

4
  • 2
    You should use while x<=1001, because x is being increased by 3; which means it will reach 999 once, and, as the initial value of x is 1, it will be 1000; and in the next iteration it will be 1003. So x will never be 1001 and the loop will never terminate.
    – mshsayem
    Feb 8, 2018 at 2:51
  • 3
    when will x == 1001?
    – Lee Gaines
    Feb 8, 2018 at 2:52
  • Thanks guys. I appreciate the help. Feb 8, 2018 at 2:59
  • and your print should be indented to the same level as the line x+=3
    – ajax_velu
    Feb 8, 2018 at 3:08

4 Answers 4

5

Your while loop never gets the condition x != 1001 evaluated to True. I checked last few values of x and those are

994
997
1000
1003

As you see the value of x never becomes 1001. So to terminate the condition when x is going to surpass 1001 you need to modify the conditon as following.

 while x <= 1001:
    num += x
    x += 3        
 print(num)
0
5

You miscalculate the expected value, x can never be 1001, The number around 1001 is 1000 and 1003, so the while loop goes forever.

I think you may use:

while x != 1000:

or:

while x < 1001:

Note as @idjaw pointed out, using != here is not a very good choice.

3
  • I would probably opt heavily for the latter. Much safer. If for whatever reason the initial value changes, you risk missing that 1000 as easily as what happened with the original problem. You know you don't want to go beyond 1001, so using the less than should be the way to go here.
    – idjaw
    Feb 8, 2018 at 3:02
  • @idjaw well, I kept that one because his code use !=, and I thought maybe he did it for some reasons. Feb 8, 2018 at 3:04
  • Sure. Just wanted to add the extra comment about what might be the better of the two because of the risks of using the first one.
    – idjaw
    Feb 8, 2018 at 3:05
2

x won't take the value 1001 ever. It becomes 1000 and then 1003 in the next iteration, so the loop continues to go on forever. while x<=1001: Can be used to resolve this.

0

x Would never be 1001 so It would run forever. If you want to make sure bring the print statement within loop and print the value of x

num = 0
x = 1
while x != 1001:
    num += x
    x += 3
    print(x)

It would print the value of x. give ctrl+c once it crossed 1000.

4
7
10
13
16
19
22
25
28
31
34
37
40
43
46
49
52
55
58
61
64
67
70
73
76
79
82
85
88
91
94
97
100
103
106
109
112
115
118
121
124
127
130
133
136
139
142
145
148
151
154
157
160
163
166
169
172
175
178
181
184
187
190
193
196
199
202
205
208
211
214
217
220
223
226
229
232
235
238
241
244
247
250
253
256
259
262
265
268
271
274
277
280
283
286
289
292
295
298
301
304
307
310
313
316
319
322
325
328
331
334
337
340
343
346
349
352
355
358
361
364
367
370
373
376
379
382
385
388
391
394
397
400
403
406
409
412
415
418
421
424
427
430
433
436
439
442
445
448
451
454
457
460
463
466
469
472
475
478
481
484
487
490
493
496
499
502
505
508
511
514
517
520
523
526
529
532
535
538
541
544
547
550
553
556
559
562
565
568
571
574
577
580
583
586
589
592
595
598
601
604
607
610
613
616
619
622
625
628
631
634
637
640
643
646
649
652
655
658
661
664
667
670
673
676
679
682
685
688
691
694
697
700
703
706
709
712
715
718
721
724
727
730
733
736
739
742
745
748
751
754
757
760
763
766
769
772
775
778
781
784
787
790
793
796
799
802
805
808
811
814
817
820
823
826
829
832
835
838
841
844
847
850
853
856
859
862
865
868
871
874
877
880
883
886
889
892
895
898
901
904
907
910
913
916
919
922
925
928
931
934
937
940
943
946
949
952
955
958
961
964
967
970
973
976
979
982
985
988
991
994
997
1000
1003
1006

As you clearly see the x never become 1001. Thats why the loop run forever. As others say change the condition to x <= 1001 which would end you loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.