-1

Why this doesn't stop at p=5? When the q counter hits the 1 it should stop, right? Sorry, it's late and I'm getting frustrated.

... q=0
... p=1
... while q < 1:
...     for range in [0,10]:
...         if p < 10:
...             print p,"elso"
...             if p > 2:
...                 print p, "masodik"
...                 if p > 4:
...                     print p, "sikerult"
...                     q+=1
...                     p+=1
...                 else:
...                     p+=1
...             else:
...                 p+=1
...         else:
...             print p, "nem jo"
...             p+=1
...             
1 elso
2 elso
3 elso
3 masodik
4 elso
4 masodik
5 elso
5 masodik
5 sikerult
6 elso
6 masodik
6 sikerult
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Gary
  • 209
  • 1
  • 3
  • 13

2 Answers2

1

A while loop does not immediately halt at any line of code within it once the condition is no longer satisfied. It halts when the execution returns to the line containing while and the condition is found to be false. Your program doesn't get around to checking the while until the for loop finishes.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • Could you fix it for me to stop when the condition is no longer satisfied? Thanks – Gary Sep 22 '15 at 07:56
  • The simplest solution is to use `break` after the part where you know q will be unacceptable. (`if q > 1: break`). But if you have nested loops (like here), you'll have to either extract the code to a function and use `return` instead of `break`, or raise some Exception and handle it outside the `while` loop. – Veky Sep 22 '15 at 07:59
  • @GergelyPuskás you've developed some serious fundamental misunderstandings that will not be solved by us fixing your code. You need to go back to the basics, read about loops more carefully, and experiment with some toy programs of your own to see if they behave as you expect. This program is a bit too messy for learning. I'm guessing you're taking a course in which you're still going to write a fair amount of code. If you want to be able to handle it then work hard on your foundations now. – Alex Hall Sep 22 '15 at 08:18
0

The statement for range in [0,10]: doesn't do what you think it does. It creates a for loop inside your while loop. That for loop sets the variable named range to 0 on the first loop and then sets range to 10 on the next loop. Then that loop ends, and the next loop of the while loop starts.

So just remove that for statement to make your code behave like you want it to. I've optimized you code slightly by getting rid of various redundant p+=1 statements.

q = 0
p = 1
while q < 1:
    if p < 10:
        print p, "elso"
        if p > 2:
            print p, "masodik"
            if p > 4:
                print p, "sikerult"
                q+=1
    else:
        print p, "nem jo"
    p+=1

output

1 elso
2 elso
3 elso
3 masodik
4 elso
4 masodik
5 elso
5 masodik
5 sikerult

This code can be improved, using a for loop that calls the range() function (which I guess is what you were trying to do with the for range in [0,10]: statement).

for p in range(1, 10):
    print p, "elso"
    if p > 2:
        print p, "masodik"
        if p > 4:
            print p, "sikerult"
            break

This code produces the same output as the previous code.

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182