我正在为一个简单的游戏制定一个解决方案,您可以选择玩家数量,最多 5 名玩家,最少 2 名玩家。每个玩家都通过他们的名字和姓氏来识别。
max_players = int(input(" Insert the number of players there are there? : "))
while len(players_list) < max_players:
player1 = input(" What is your first and last name? : ")
players_list.append(players)
print("players so far : ")
player2 = input ("What is your first and last name? :")
players_list.append(players)
print("players so far : ")
print(players_lists)
代码部分有效。问题是,虽然我提到了玩家的最大数量,但你仍然可以取消大于 5 的数字。另外,当我插入玩家的名字时,它显示“玩家”没有定义? 预期输出为
Hello how many players are in the game?
insert number between 2 and 5
Please insert your name
然后对其他玩家使用相同的命令。
最佳答案
Also, when I insert the first name of the player, it shows that the "players" are not defined
您尝试将 players
(在调用它之前确实不存在)附加到 players_list
(它也不存在)。您需要首先定义这两个。
对于玩家数量限制,您可以添加一个简单的检查,即 max_players
是否在 2 到 5 之间。
max_players = int(input(" Insert the number of players there are ? : "))
while (max_players <2) or (max_players > 5) :
max_players = int(input(" Number of players must be between 2 and 5. Number of players ? "))
players_list = []
while len(players_list) < max_players:
player1 = input(" What is your first and last name? : ")
players_list.append(player1)
print("players so far : ")
print(players_list)
关于python - 在互动游戏中分配玩家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74562397/