Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop
#1
Im a beginner programmer trying to learn Python and taking an video intro course. I don't have any other resources to ask, and found this forum.
I almost have to apologize for this but its bugging me to know what the problem is instead of moving on.
This is a pretty simple while loop.
I am using PyCharm 3.8, and the tutor in the video is using PyCharm 3.5.
The screens and output appear to be the same between the programmer and I.
Code is exactly the same but output is different in the second case.
Below is the first example, that works great, simple enough:
c=0
>>> while c<5:
...     print(c)
...     c=c+1
...    
1
2
3
4
Programmer changes the code and adds a break statement that ends the output at 3, and output is clearly correct in the video:
c=0
while c<5:
    print(c)
    if c == 3:
        break
        c=c+1
     
0
1
2
3
I run this code and i get continuous stream of 0s that I have to ctl-c out of.
I understand what it is supposed to do but don't understand why its running a different output from what I can obviously see as the 0,1,2,3 then break, in the video. There are no errors just a continuous stream.
Thank you in advance
Reply
#2
look at your second script,
the only time c will get incremented is if c is equal to 3
So, if it can't get incremented, it can't ever reach 3
You need to change indentation
c=0
while c<5:
    print(c)
    if c == 3:
        break
    c=c+1
Reply
#3
Wow, I feel a little dumb at the moment.
Punctuation I understood, didn't realize the indents affected the code at times.
I did rerun and the output is now correct.
Many thanks, I can sleep well tonight ;)
Anthony
Reply
#4
Quote:affected the code at times.
always!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020