Я пытаюсь создать цикл, который будет вращаться среди пользователей и получать действительные данные.Входные данные - это словарные слова, проверяемые двумя внешними файлами, один из которых содержит разрешенные слова, а другой ограничен.Это игра, в которой пользователи должны произносить слово, начинающееся с буквы последнего слова оппонента.Вы получаете до 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
´´´´