简体   繁体   中英

While Loop Won't Break?

This is just a quick little code task for school, but I've been testing and changing all the possible ways the condition could be and nothing works. The while loop just continues to run past 9 turns, which it shouldn't be doing. It's such a simple while loop yet it doesn't break and it's really confusing me. I'm probably being super dumb but please help.

def main():
    turns=0
    while turns<9:
        print("Turns:",turns)
        print_grid()
        p1()
        turns+=1
        print("Turns:",turns)
        print_grid()
        p2()
        turns+=1
    print("done")
main()

There are obviously unecessary print statements, they're just the ones I was using to check the turns to make sure I wasnt being stupid. This is the most basic way I can tell it to continue to loop until turns equals 9, which when the code runs, it literally tells me in IDLE - "Turns: 9" which just angers me further. I cant input further than 9 due to the rest of the code not allowing further inputs.

There is a missing function print_grid but from your description I think what you need is this

def main():
    turns=0
    while turns<9:
        print("Turns:",turns)
        turns+=1 # to iterate and print turns, it should be after **print**
    print("done")
main()

output

Turns: 0
Turns: 1
Turns: 2
Turns: 3
Turns: 4
Turns: 5
Turns: 6
Turns: 7
Turns: 8
done

The condition of the while loop is only checked before each iteration runs. If turns becomes greater than 9 at some point in the middle of the loop, the loop will not break half way through an iteration. If you want it to break in the middle, you need to manually check that:

while turns<9:
    print("Turns:",turns)
    print_grid()
    p1()
    turns+=1
    if turns >= 9:  # Check here
        break
    print("Turns:",turns)
    print_grid()
    p2()
    turns+=1

This is messy though. Most of that loop is duplicated. The only thing that changes is the call to p1 and p2 . I'd just toggle between the two functions, and change from a while to a range :

func = p1
for turn in range(9):
    print("Turns:", turn)
    print_grid()
    func()  # Call p1 or p2 depending

    if func is p1:  # Toggle
        func = p2
    else:
        func = p1

That end part can be changed to this as well:

func = p2 if func is p1 else p1

You should probably convert that while loop into a for loop as it's better for your usecase but let me explain what happens anyway.

During your loop, you increment turns after you loop begins, so it will go up to 9, and THEN break. What you can do to fix that is change the code from while turns < 9 to while turns != 8 or introduce an if statement

if turns == 8:
    break

Then again, I suggest you change it into a for loop:

def main():
    turns=0
    for _ in range(8):
        print("Turns:",turns)
        print_grid()
        p1()
        turns+=1
        print("Turns:",turns)
        print_grid()
        p2()
        turns+=1
    print("done")
main()

EDIT: Like @CYREX said, you should also increment before printing, not after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM