I'm trying to write a simple code to help me keep track of player names and points when playing card games.
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)
Now I need help finding a way to sum the points for each round/each player. I also want to keep updating the current_round until something like input('Keep Playing?')
is 'No'.
Any ideas?
Thank you
I would set up your dictionary d
to have player IDs as keys, and points as values, like so:
d = {k: v for k,v in zip(p_list, points)}
Then when player with ID j
'scores' n
points, you can just do d[j] += n
Here's your game set up that way (you probably know how to do the necessary to print a nice dataframe instead of the raw dictionary in my code). I added some sample code to handle the optional further rounds.
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()