2 plater die game in python

Solutions on MaxInterview for 2 plater die game in python by the best coders in the world

showing results for - "2 plater die game in python"
Martina
23 Feb 2019
1import random
2
3def get_name(key):
4    n = ""
5    while not n:
6        n = input(f"Input your name, {key}: ")
7    return n
8
9def throw_dice(key):
10    dices = random.choices(range(1,7),k=2)
11    print(f"{names[key]} threw: {dices}", end = " ")
12    if sum(dices) % 2 == 0:
13        score[key] += 10
14        print("Even. You earn 10 points.")
15    else:
16        score[key] -= 5
17        print("Odd. You loose 5 points")
18
19def print_score():
20    for n in score:
21        print(f"  {names[n]} has got {score[n]} points.")
22
23def win_message(s,n):
24    print_score()
25    if score["P1"] > score["P2"]:
26        print(f"{names['P1']} won")
27    else:
28        print(f"{names['P2']} won") 
29
30
31
32player = ""
33score = {}
34names = {}
35
36for n in ["P1","P2"]:
37    names[n] = get_name(n)
38    score[n] = 0
39
40# twice the amount of rows wanted, because players take turn
41
42for c in range(10):
43    # switches between player1 and player2
44    player = "P1" if player != "P1" else "P2"
45
46    print(f"Round {c//2 + 1}: it is {names[player]}'s turn.")
47    print_score()
48    throw_dice(player)
49
50win_message(score,names)