为游戏中的多个玩家添加积分的代码

Code to add points for multiple players in a game

我正在尝试编写一个简单的代码来帮助我在玩纸牌游戏时跟踪玩家姓名和分数。

import pandas as pd

print('Willkommen zum Punkterechner!')

#asking for number of players 
p_count = int(input("Anzahl der Spieler?: "))
p_list = []
points = [0]*p_count

#asking for name of players in range of number of players 
for i in range(p_count):
    p_name = input('Wie lautet der Name von Spieler ' + str(i+1) + '?: ')
    p_list.append(p_name)

d = {'Spieler': p_list, 'Punkte': points}
df = pd.DataFrame(data=d)
print(df)

current_round = 1

for i,j in enumerate(p_list):
    points_round = int(input('Punkte für ' + j + ' in Runde ' + str(current_round) + ': '))
    points[i] += points_round

print(df)

现在我需要帮助找到一种方法来计算每个 round/each 玩家的分数。我还想继续更新 current_round 直到 input('Keep Playing?') 变成 'No'.

有什么想法吗?

谢谢

我会设置你的字典 d 将玩家 ID 作为键,将点数作为值,如下所示:

d = {k: v for k,v in zip(p_list, points)}

那么当ID为j 'scores' n的玩家获得积分时,你可以d[j] += n

这是你的游戏设置方式(你可能知道如何打印一个漂亮的数据框而不是我的代码中的原始字典)。我添加了一些示例代码来处理可选的进一步回合。

import pandas as pd
import operator

print('Willkommen zum Punkterechner!')

#asking for number of players 
p_count = int(input("Anzahl der Spieler?: "))
p_list = range(1, p_count+1)
points = [0]*p_count
d = {k: v for k,v in zip(p_list, points)}

def play_round(current_round=1):
    for player in d.keys():
        points_round = int(input('Punkte für ' + str(player) + ' in Runde ' + str(current_round) + ': '))
        d[player] += points_round
    print(d)

    if input('Noch ein Spiel?') == 'Nein':
        winner = max(d.items(), key=operator.itemgetter(1))[0]
        print(f'Der Gewinner ist {winner} mit {d[winner]} Punkten!') 
        return
    else:
        play_round(current_round+1)

play_round()