Answer:
import random
def roll_dice(r):
score1 = 0
score2 = 0
for i in range(r):
for j in range(5):
score1 += random.randint(1,6)
score2 += random.randint(1,6)
avg1 = score1/5
avg2 = score2/5
if avg1 > avg2:
print("Player1 won the " + str(i+1) + ". round ")
elif avg1 < avg2:
print("Player2 won the " + str(i+1) + ". round ")
else:
print("Tie")
score1 = 0
score2 = 0
roll_dice(3)
Explanation:
Import the random module
Create a function called roll_dice that takes one parameter r
Initialize two scores as 0 for the players
Create a nested for loop. The outer loop will simulate the rounds and inner loop will simulate the each roll.
Inside the inner loop which iterates five times, generate random numbers for two players and add these numbers to their score.
When the inner loop is done, calculate the average score of the players. If the first average is greater, player1 wins the round. If the second average is greater, player2 wins the round. Otherwise, it is a tie. Also, set the scores to zero so that they start again.
Call the function with parameter 3, simulating 3 rounds