【问题标题】:While loop not breaking(python)虽然循环不中断(python)
【发布时间】:2019-08-26 13:24:49
【问题描述】:

这里根据条件设置的值。如果 cows 等于 4 那么 while 循环应该中断。 但这里 break 被视为不存在。

import random

r = random.randint
def get_num():
 return "{0}{1}{2}{3}".format(r(1, 9), r(1, 9), r(1, 9), r(1, 9))

n = get_num()
print(n)
n = [z for z in str(n)]

def game():
    cows = 0
    bulls = 0
    print()

    usr_num = [i for i in input("enter:\n")]
    usr_set = set(usr_num)

    while True:
        for x in usr_set:
            if usr_num.count(x) >= n.count(x):
                cows += n.count(x)
                bulls += usr_num.count(x) - n.count(x)
            elif usr_num.count(x) < n.count(x):
                cows += usr_num.count(x)
                bulls += n.count(x) - usr_num.count(x)

        print("cows: ", cows, "   bulls: ", bulls)

        if cows == 4:
            print("correct!")
            break
        else:
            game()

game()

cows = 4 时,correct 被打印,但 break 没有显示效果

如果我们稍微改变一下代码。 如果我们把 4(If 语句)

代替 cows
def game():
    cows = 0
    bulls = 0
    print()

    usr_num = [i for i in input("enter:\n")]
    usr_set = set(usr_num)

    while True:
        for x in usr_set:
            if usr_num.count(x) >= n.count(x):
                cows += n.count(x)
                bulls += usr_num.count(x) - n.count(x)
            elif usr_num.count(x) < n.count(x):
                cows += usr_num.count(x)
                bulls += n.count(x) - usr_num.count(x)

        print("cows: ", cows, "   bulls: ", bulls)

        if 4 == 4:
            print("correct!")
            break
        else:
            game()

game()

那么 break 就起作用了。

【问题讨论】:

  • 欢迎来到Stack OverflowNameError: name 'n' is not definededit 您的问题包含足够的代码,以便我们可以复制问题。请同时包含一些示例输入。
  • 由于递归,你同时处于多个循环中。
  • game() 设置一对 local 变量,而不是循环中使用的变量。 (或者你的代码中的缩进有问题吗?)
  • 我们无法复制您的问题,因为您没有提供minimal reproducible example:如果您需要帮助,您必须提供丢失的代码以及产生问题的有效输入。还请非常仔细检查以确保缩进正确。

标签: python while-loop break


【解决方案1】:

每次执行另一轮时,您都在递归,这意味着当您 break 时,您最终只会跳出上一次递归。

不要使用尾递归,而是尝试移动while True:

def game():
    while True:
        cows = 0
        bulls = 0
        print()

        usr_num = [i for i in input("enter:\n")]
        usr_set = set(usr_num)

        for x in usr_set:
            if usr_num.count(x) >= n.count(x):
                cows += n.count(x)
                bulls += usr_num.count(x) - n.count(x)
            elif usr_num.count(x) < n.count(x):
                cows += usr_num.count(x)
                bulls += n.count(x) - usr_num.count(x)

        print("cows: ", cows, "   bulls: ", bulls)

        if cows == 4:
            print("correct!")
            break

这样我们就不会递归了,所以我们的 break 就像你期望的那样工作:见repl.it

【讨论】:

    【解决方案2】:

    我刚刚尝试运行您的代码,但您的脚本存在更多问题,而不仅仅是 while 循环。

    但是试试这个小脚本来了解 while 循环是如何工作的:

    # While loop test
    
    i=0
    j=5
    while True:
        if i >= j:
            break
        else:
            print(f"{i} < {j}")
            i +=1
    

    希望这会有所帮助。玩一玩。

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 2016-06-14
      • 2021-12-22
      • 1970-01-01
      • 2020-03-17
      • 2011-12-03
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多