Skip to content

Instantly share code, notes, and snippets.

@nawarazpokhrel
Created December 11, 2022 13:07
Show Gist options
  • Save nawarazpokhrel/45563f9b2327dbf938d29cfa7210dd45 to your computer and use it in GitHub Desktop.
Save nawarazpokhrel/45563f9b2327dbf938d29cfa7210dd45 to your computer and use it in GitHub Desktop.
python dice game
import random
def get_players_numbers():
try:
player_number = int(input("Please enter the number of players: "))
return player_number
except ValueError:
print("print provide alpha numeric or numeric value of players numbers")
def get_number_of_rounds(rounds):
if rounds == '':
raise TypeError("please pass integer not empty value")
try:
rounds = int(rounds)
return rounds
except ValueError:
print("print provide alpha numeric or numeric value of rounds")
def play_game():
flag = True
while flag:
players_name = []
print("""
***** Welcome to another Dice Rolling Game *****""")
for player in range(get_players_numbers()):
player_name = (input(f"Please enter player #{player + 1} name "))
players_name.append(player_name)
total_rounds = get_number_of_rounds(input(""" Please enter how many rounds the players wish to play: """))
for rounds in range(total_rounds):
print(f""" ********Round {rounds + 1}****""", """ """, end='')
print("Total")
print()
for player in players_name:
print(player)
print()
per_round_score = {}
winner_dict = {}
for rounds in range(total_rounds):
players_score = {}
for player in players_name:
user_input = input(f"""{player}! Hit enter once you are ready to roll your dices!""")
if user_input == "":
score = []
for _ in range(0, 2):
number = random.randint(1, 6)
score.append(number)
players_score.update({
f"{player}": sum(score)
})
print(score[0], "and", score[1])
per_round_score.update({
f"rounds{rounds}": players_score
})
print(f""" ************************ End of Round {rounds + 1} ********************""")
print("""
""")
players = list(per_round_score['rounds0'].keys())
rounds = range(len(per_round_score.keys()))
print(f" {''.join(f' ****Round {rd + 1}**** ' for rd in rounds)} **** Total ****")
for player in players:
scores = [per_round_score[f'rounds{rd}'].get(player, 0) for rd in rounds]
print(f"{player:10s}{''.join(f'{s:^20d}' for s in scores)}{sum(scores):^15d}")
players = list(per_round_score['rounds0'].keys())
rounds = range(len(per_round_score.keys()))
for player in players:
scores = [per_round_score[f'rounds{rd}'].get(player, 0) for rd in rounds]
winner_dict.update({player: sum(scores)})
max_value = max(winner_dict.values())
max_score_name = max(winner_dict, key=lambda x: winner_dict[x])
if len({i for i in winner_dict if winner_dict[i] == max_value}) > 1:
print("we have two winner game drawn! why not play again to find new winner :)")
else:
print(f"Congratulations {max_score_name}! You are our WINNER!")
print("Would you like to play another game?")
print("[1] Yes ")
print("[2] No ")
end_game_input = input("Your Input: ")
if end_game_input == "2":
flag = False
print("Thank you and see you later!")
play_game()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment