Ask Question
10 May, 07:43

Create a function that should simulate a game involving the rolling of a standard six-sided dice between two players. The function takes in the integer number of rounds, which corresponds to how many games to play. Each game consists of both players rolling the dice five times each, whichever player has the highest average die roll from their five rolls wins the round.

+5
Answers (1)
  1. 10 May, 09:25
    0
    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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Create a function that should simulate a game involving the rolling of a standard six-sided dice between two players. The function takes in ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers