简体   繁体   English

虽然循环不会中断?

[英]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. while 循环只是继续运行超过 9 圈,这是它不应该做的。 It's such a simple while loop yet it doesn't break and it's really confusing me.这是一个如此简单的 while 循环,但它并没有中断,这让我很困惑。 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.这是我可以告诉它继续循环直到轮数等于 9 的最基本的方法,当代码运行时,它在 IDLE 中直接告诉我 - “轮数:9”,这只会进一步激怒我。 I cant input further than 9 due to the rest of the code not allowing further inputs.由于其余代码不允许进一步输入,我无法输入超过 9。

There is a missing function print_grid but from your description I think what you need is this缺少一个函数print_grid但从你的描述中我认为你需要的是这个

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.仅在每次迭代运行之前检查while循环的条件。 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.如果在循环中间的某个点turns大于9 ,则循环不会在迭代中途中断。 If you want it to break in the middle, you need to manually check that:如果您希望它在中间break ,则需要手动检查:

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 .唯一改变的是对p1p2的调用。 I'd just toggle between the two functions, and change from a while to a range :我只是在两个函数之间切换,然后从一段while更改为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.您可能应该将该while循环转换为for循环,因为它更适合您的用例,但让我解释一下无论如何会发生什么。

During your loop, you increment turns after you loop begins, so it will go up to 9, and THEN break.在循环过程中,循环开始后增加turns ,所以它会上升到 9,然后中断。 What you can do to fix that is change the code from while turns < 9 to while turns != 8 or introduce an if statement你可以做些什么来解决这个问题是将代码从while turns < 9更改为while turns != 8或引入 if 语句

if turns == 8:
    break

Then again, I suggest you change it into a for loop:再说一次,我建议您将其更改为for循环:

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.编辑:就像@CYREX 所说的,你也应该在打印之前而不是之后增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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