嵌套while循环,为什么此循环没有循环回到顶部?

时间:2019-02-09 22:11:33

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

我正在尝试创建一个循环,该循环将在用户之间轮换并获得有效的输入。输入是使用外部两个文件检查的字典单词,其中两个文件包含允许的单词,另一个包含受限制的单词。在这种游戏中,用户必须说出一个以对手最后一个词的结尾字母开头的单词。语气不足时,您最多可获得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   

´´´

1 个答案:

答案 0 :(得分:0)

正如Nico238在评论中所说,您在每个if / else语句中使用return语句中断循环。由于此游戏依赖于逻辑上的进展(玩家是否输入了正确的第一个字母,并且是否在字典中),因此您需要仔细处理循环,最好使循环尽可能平整(不嵌套),并且当然不要中断如果您需要留在游戏循环中,请选择其中的一个。

我的建议是删除那些return start_letter语句,因为您已经将其设置为非空值,这将继续您的循环。