【问题标题】:How to fix repeating while loop in Python如何修复Python中的重复while循环
【发布时间】:2019-07-05 03:08:20
【问题描述】:

我正在尝试使用 while 循环在两个用户之间交替轮流,但我的代码卡在“while first_player_move is True:”循环中。我怎样才能解决这个问题,让我的 while 循环遍历两个玩家的回合。

我尝试在不同的地方添加“继续”和“中断”,并尝试向上切换布尔值,但似乎没有任何效果。

word_fragment = ''
        first_player_move = True
        while True:
            while first_player_move is True:
                added_letter = input('Which single letter would you like to add to the fragment? ')
                word_fragment += added_letter
                print('The current word fragment is: ' + word_fragment)
                print('It is now ' + player2_name + "'s turn.")
                if word_fragment in open('data.txt').read() and len(word_fragment) > 3:
                    print('I am sorry, you just lost. ' + player2_name + ' is the winner!')
                    # call a function to end the game
                    break

            while first_player_move is False:
                added_letter = input('Which single letter would you like to add to the fragment? ')
                word_fragment += added_letter
                print('The current word fragment is: ' + word_fragment)
                print('It is now ' + player1_name + "'s turn.")
                if word_fragment in open('data.txt').read() and len(word_fragment) > 3 :
                    print('I am sorry, you just lost. ' + player1_name + ' is the winner!')
                    # call a function to end the game
                    break

我希望输出贯穿每个玩家的回合并最终打印“现在是'下一个玩家'的回合”,但它会继续为下一个玩家回合打印相同的名称,这告诉我代码卡在两个 while 循环中的第一个。

【问题讨论】:

  • 使用if 代替while 并将first_player_move 重置为false
  • 成功了,非常感谢!
  • 关于is True 的一些东西:起初绝对不需要,如果你有布尔值if(或while)条件在if p: 上的行为与@987654330 上的行为相同@。许多 Python 函数返回“真实”对象,这些对象被认为是真实的 (bool(p) == True),但不完全是 True。对于这样的对象,is True 将失败。最后但并非最不重要的一点是is 运算符正在测试双方是否是完全相同的对象。仅因为 CPython 解释器中的优化,这适用于您的情况。使用== 比较值!

标签: python python-3.x while-loop


【解决方案1】:

由于first_player_move不会变成false,所以当内层循环结束时,外层开始一个新的循环并再次调用内层。

顺序流是:

  1. 进入第一个循环 => True # 太真实了
  2. 进入内循环 => first_player_moveTrue # 太真实了
  3. 执行内部块然后breaks并进入第一个循环并重复上述步骤

  4. 进入第一个循环 => True # so true

  5. 进入内循环 => first_player_moveTrue # 太真实了

【讨论】:

  • 感谢分解,帮了大忙!
【解决方案2】:

以下方法可能效果更好。目的是尽量避免重复代码,而是使用变量来帮助:

word_fragment = ''
first_player_move = True

while True:
    player_name = player1_name if first_player_move else player2_name
    print(f"It is now {player_name}'s turn.")

    added_letter = input(f'{player_name}: Which single letter would you like to add to the fragment? ')
    word_fragment += added_letter

    print('The current word fragment is: ', word_fragment)

    if word_fragment in open('data.txt').read() and len(word_fragment) > 3:
        print('I am sorry, you just lost. ' + player2_name + ' is the winner!')
        break  # call a function to end the game

    first_player_move = not first_player_move

这使用player_name 保存当前玩家的姓名,first_player_move 在每个循环中在TrueFalse 之间切换。

【讨论】:

  • 哇,谢谢你,我从来没有那样考虑过我的问题。我仍然是初学者,非常感谢您的帮助!
【解决方案3】:

您永远不会在第一个循环中将 first_player_move 设置为 false(也永远不会在第二个循环中将其设置为 true)。

我建议将 print('It is now ' + player2_name + "'s turn.") 移动到您的 if 中并进行修改:

if word_fragment in open('data.txt').read() and len(word_fragment) > 3:
    print('I am sorry, you just lost. ' + player2_name + ' is the winner!')
    # call a function to end the game
    break
else:
    print('It is now ' + player2_name + "'s turn.")
    first_player_move = False

与第二个玩家循环相当的模组。

我还想确认您的赢/输条件。他们以我阅读您的代码的方式,如果有人负责创建包含在 data.txt 中的片段并且该片段大于 3 个字符,他们就会丢失。这是正确的吗?

如果是这样,您还可以进行一些优化以缩小 data.txt 的大小。去掉所有 3 个或更少字符的单词,您可以删除 len(word_fragment) > 3 约束。

我希望您会遇到另一个问题......如果玩家获得相当大的片段但仍然没有匹配 data.txt 中的任何内容,会发生什么?

您可能需要考虑创建平局条件。例如。在“while True”主循环结束时,测试长度 > data.txt 中最长的单词并将其称为平局。

进一步的风格建议。玩家回合无需使用循环:

  • 将整个事情保持在您的大“while True”循环中:
  • 提示玩家 1 输入
  • 测试 player1 丢失(即 data.txt 中的片段)
  • 提示玩家 2 输入
  • 测试 player2 损失
  • 测试片段长度 > 最大长度
  • 回到循环顶部
  • 玩家丢失或最大片段长度中断
  • 无需切换 first_player_move True/False

如果您只使用一段 prompt_then_test 代码并在每次通过时在玩家名称之间切换,则可以获得奖励积分。

【讨论】:

  • 您好,感谢您的帮助!我仍然是一个初学者,所以我能得到的所有帮助都非常感谢。是的,如果有人在“data.txt”中创建了一个单词片段,他们就会丢失。这是我正在学习的课程的编程作业。
  • 没问题。我还有另一种风格建议。使用“While True”+“Break”的循环可能难以阅读。我更喜欢: endCondition = False While endCondition = False:
  • 我还没有弄清楚这里的 cmets 中是否可以有段落。更易于阅读的版本:使循环依赖于 endCondition = True。在循环之前将其设置为 False,然后在满足条件时在循环内将其设置为 True。
  • P.S.现在已经过去了几天,您应该选择一个答案。我的偏见是选择我的。我的公正意见是一个艰难的决定。 Martin Evans 的方法包括我每次重复使用相同代码的奖励积分建议。这个游戏可能没有意义,但您甚至可以扩展该方法以使用玩家列表并在继续进行时消除它们。例如。比赛开始时有 4 名玩家,一名玩家犯错,名单下降到 3 人,然后是 2 人,然后是获胜者。
猜你喜欢
  • 2022-06-15
  • 2020-09-15
  • 2014-08-29
  • 2012-07-03
  • 2021-09-09
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多