In [1]:

```
from random import random, choice
```

In [2]:

```
def main():
print_intro()
prob_a, prob_b, n = get_inputs()
wins_a, wins_b = sim_n_games(n, prob_a, prob_b)
print_summary(wins_a, wins_b)
```

In [3]:

```
def print_intro():
print("This plays racquetball, blah blah blah…")
```

In [4]:

```
print_intro()
```

This plays racquetball, blah blah blah…

In [7]:

```
def get_inputs():
a = float(input("What is the probability Player A will win a serve? "))
b = float(input("What is the probability Player B will win a serve? "))
n = int(input("How many games ot simulate? "))
return a, b, n
```

In [8]:

```
get_inputs()
```

What is the probability Player A will win a serve? 0.7 What is the probability Player B will win a serve? 0.65 How many games ot simulate? 10_000

Out[8]:

(0.7, 0.65, 10000)

In [10]:

```
def sim_n_games(n, prob_a, prob_b):
"""Simulates n games of racquetball
n (int): the number of games to play
prob_a (float): the service probability for player A
prob_b (float): the service probability for player B
returns int, int
respectively represening the total number ofgames won by players A and B
"""
wins_a = 0
wins_b = 0
for i in range(n):
score_a, score_b = sim_one_game(prob_a, prob_b)
if score_a > score_b:
wins_a += 1
else:
# score_b > score_a:
wins_b += 1
return wins_a, wins_b
```

In [24]:

```
sim_n_games(10_000, 0.6, 0.65)
```

Out[24]:

(4004, 5996)

In [13]:

```
def print_summary(wins_a, wins_b):
n = wins_a + wins_b
print(f"Games simulated: {n}")
print("Wins for A: {0} ({1:0.1%})".format(wins_a, wins_a/n))
print("Wins for B: {0} ({1:0.1%})".format(wins_b, wins_b/n))
```

In [14]:

```
print_summary(45, 55)
```

Games simulated: 100 Wins for A: 45 (45.0%) Wins for B: 55 (55.0%)

In [15]:

```
def sim_one_game(prob_a, prob_b):
"""Simulates a *single* game of racquetball
prob_a (float): the service probability for Player A
prob_b (float): the service probability for Player B
returns int, int
(the final scores for each player)
"""
serving = "A"
score_a = 0
score_b = 0
#while is_game_over(score_a, score_b) == False:
#while is_game_over(score_a, score_b) is False:
while not is_game_over(score_a, score_b):
if serving == "A":
# This is the Monte Carlo part, getting a random
# number between [0..{prob_val}) represent a winning serve
if random() < prob_a:
score_a += 1
else:
# Player A lost the serve, no points awarded.
serving = "B"
else:
# elif serving == "B"
if random() < prob_b:
score_b += 1
else:
# Player B lost the serve, no points awarded.
serving = "A"
return score_a, score_b
```

In [25]:

```
sim_one_game(0.6, 0.7)
```

Out[25]:

(13, 15)

In [26]:

```
def is_game_over(a, b):
"""Determine if a racquetball game is finished
a (int): score for Player A
b (int): score for Player B
returns True if game is over, False if not
"""
# Option 1:
# if a == 15:
# return True
# elif b == 15:
# return True
# else:
# return False
# Option 2:
# if a == 15 or b == 15:
# return True
# else:
# return False
# Option 3:
#result = not (a < 15 and b < 15)
#result = (a >= 15 or b >= 15)
#return result
# Option 4:
#return True if (a >= 15 or b >= 15) else False
# Option 5:
return a == 15 or b == 15
```

In [18]:

```
a = 7
b = 3
```

In [19]:

```
a == 15 or b == 15
```

Out[19]:

False

In [ ]:

```
7 == 15 or 3 == 15
False or False
False
```

In [20]:

```
a = 15
b = 8
```

In [21]:

```
a == 15 or b == 15
```

Out[21]:

True

In [23]:

```
15 == 15 or 8 == 15
True or False
True
```

Out[23]:

True

In [27]:

```
is_game_over(7, 15)
```

Out[27]:

True

In [28]:

```
is_game_over(3, 7)
```

Out[28]:

False

In [29]:

```
main()
```

In [30]:

```
main()
```

In [31]:

```
main()
```

In [32]:

```
def sim_one_game(prob_a, prob_b):
"""Simulates a *single* game of racquetball
prob_a (float): the service probability for Player A
prob_b (float): the service probability for Player B
returns int, int
(the final scores for each player)
"""
serving = choice(["A", "B"])
score_a = 0
score_b = 0
#while is_game_over(score_a, score_b) == False:
#while is_game_over(score_a, score_b) is False:
while not is_game_over(score_a, score_b):
if serving == "A":
# This is the Monte Carlo part, getting a random
# number between [0..{prob_val}) represent a winning serve
if random() < prob_a:
score_a += 1
else:
# Player A lost the serve, no points awarded.
serving = "B"
else:
# elif serving == "B"
if random() < prob_b:
score_b += 1
else:
# Player B lost the serve, no points awarded.
serving = "A"
return score_a, score_b
```

In [33]:

```
main()
```

In [34]:

```
main()
```

In [35]:

```
main()
```

In [36]:

```
main()
```

In [ ]:

```
```