如何在一个简单的游戏中分配分数



我正在制作一款简单的游戏,一开始你可以选择玩家的数量,在2到5之间(如下所示)。我在分配初始点数时遇到了问题,即100分。此外,不确定在哪里放置代码关于点在我的工作代码下面。当我开始制作游戏时,每次骰子移动后分数都会增加。

players_list= []
max_players= 5
max_players = int(input(" Please, insert the number of players? : "))
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(" Enter your first and last name? : ")
players_list.append(player1)
print("Players in the game : ")
print(players_list)

我应该把球员列表改成字典吗?

分数系统不起作用的代码

score=100
players_list= []
max_players= 5
max_players = int(input(" Please, insert the number of players? : "))
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(" Enter your first and last name? : ")
players_list.append(player1)
print("Players in the game : ")
players_list.appened (players)= { score:100}
print(players_list)
print(score)

我建议使用字典,其中键是玩家的名字(假设玩家的名字是唯一的),值是玩家的分数:

players_dict = {}
score = 100
max_players = -1
while not (2 <= max_players <= 5):
max_players = int(input("Please, insert the number of players: "))
while len(players_dict) < max_players:
player = input("Enter your first and last name: ")
if player in players_dict:
print(f"Player {player} already exists, choose another name")
else:
players_dict[player] = score
print(players_dict)

打印(例如):

Please, insert the number of players: 1
Please, insert the number of players: 3
Enter your first and last name: John
Enter your first and last name: Adam
Enter your first and last name: John
Player John already exists, choose another name
Enter your first and last name: Lucy
{'John': 100, 'Adam': 100, 'Lucy': 100}

不,您不需要将列表更改为字典。相反,你可能想要一个字典列表。TLDR;

我不确定我是否理解了你的问题,因为描述太模糊了。

  1. 不需要在while循环之前使用输入。
  2. 用户可以输入一些不能被解析成int的东西,所以我把它包装成try…除了
  3. "player_list"是重新定义
  4. 在第二个循环中它不是player1,而是"next">
  5. 你也可以保持名字和姓氏在一个字符串中,然后跳过分割
  6. 无论如何,让你的玩家成为一个字典是有意义的。考虑将players_list重命名为players,通常你不会将数据结构的名称添加到变量名代码:

score=100
players_list= []
max_players= 0
while (max_players <2) or (max_players > 5) :
try:
max_players = int(input(" Number of players must be between 2 and 5.Number of players ?"))
except Exception as e:
print(f"Invalid input: {e}")
while len(players_list) < max_players:
name, surname = input(" Enter your first and last name? : ").split(" ")
player = {"name": name, "surname": surname, "points": score}
players_list.append(player)
print(f"Players in the game : {players_list}")

最新更新