首页 > 解决方案 > 嵌套的while循环,为什么这个循环没有循环回顶部?

问题描述

我正在尝试创建一个循环,它将在用户之间轮换并获得有效的输入。输入是使用外部两个文件检查的字典单词,一个包含允许的单词,另一个包含限制的单词。这是一个游戏,用户必须说出一个以对手最后一个单词的结尾字母开头的单词。当您说不出话时,您最多可以通过 3 次通行证。当所有三个传球都通过时,您输掉了比赛。我已经多次修改了代码,但由于某种原因它没有循环:

def game_logic(players_data):
"""takes in players data and initiates the game logic.

:param players_data: contains Player objects with name, word_list and pass_taken as attributes.
:type players_data: list containing dictionaries as items.
"""
# Initialise allowed and prohibited words from external file read.
with open("docs/wordDict.txt", "r") as wordDict:
    allowed_words = wordDict.read()
with open("docs/excludeWords.txt", "r") as excludeWords:
    prohibited_words = excludeWords.read()
game_switch = True
valid_word = False
player_turn = ""
start_letter = ""
while game_switch:
    player_turn = players_data[0].name
    if not players_data:             # iff empty list
        print("Something went wrong. I could not find players! Please restart the game.")
        break
    elif len(players_data) == 1:        # when one person is left
        print(f"{players_data[0].name} wins.\nCongratulations!\n°°*°°*°°*°°*°°*°°*° ")
        print(f"beat all opponents in: {playtime - datetime.datetime.now()}")
        break
    else:
        print(f"\nIt is {player_turn.upper()}'s turn")
        # add a copy of first element to the end
        players_data.append(players_data[0])
        # remove the first element. so that next turn is next ones'.
        players_data.remove(players_data[0])
        # start the game
        while not valid_word:
            if not start_letter:
                input_word = input(f"please enter a valid word to begin: ")
                if input_word.lower() in allowed_words and input_word.lower() not in prohibited_words:
                    players_data[-1].word_list.append(input_word)
                    start_letter = input_word[-1].upper()
                    print(f"\nStarting letter for next player is: {start_letter}")
                    valid_word = True
                    return start_letter
                else:
                    players_data[-1].pass_taken += 1
                    print(f"FOUL!\nThe word was not recognised as a valid word.\nPenalty: 1 pass({3 - players_data[-1].pass_taken})")
                    print("Turn goes to your opponent.")
                    valid_word = False
                    if players_data[-1].pass_taken >= 3:
                        print(f"LOST!\n{players_data[-1].name} is out of the game")
                        players_data.pop()
            else:
                input_word = input(f"please enter a valid word begining with letter {start_letter}: ")
                if input_word.lower() in allowed_words and input_word.lower() not in prohibited_words and input_word[0].upper() == start_letter:
                    players_data[-1].word_list.append(input_word)
                    start_letter = input_word[-1].upper()
                    print(f"\nStarting letter for next player is: {start_letter}")
                    valid_word = True
                    return start_letter
                else:
                    players_data[-1].pass_taken += 1
                    print(f"FOUL!\nThe word was not recognised as a valid word.\nPenalty: 1 pass({3 - players_data[-1].pass_taken})")
                    print("Turn goes to your opponent.")
                    valid_word = False
                    if players_data[-1].pass_taken >= 3:
                        print(f"LOST!\n{players_data[-1].name} is out of the game")
                        players_data.pop()
                continue   

''''

标签: python-3.xfunctionwhile-looplogic

解决方案


正如 Nico238 在评论中所说,您在每个 if/else 语句中都使用 return 语句来打破循环。由于这个游戏依赖于逻辑进程(玩家是否输入了正确的第一个字母,以及它是否在字典中)你需要小心处理循环,最好使它们尽可能平坦(不嵌套),当然不要破坏如果您需要留在游戏循环中,请远离它们。

我的建议是删除这些return start_letter语句,因为您已经将其设置为空以外的内容,这应该会继续您的循环。


推荐阅读