Ask Question
10 May, 00:49

Create the logic for a game that simulates rolling two dice by generating two random numbers between 1 and 6 inclusive. The player chooses a number between 2 and 12 (the lowest and highest totals possible for two dice). The player then "rolls" two dice up to three times. If the number chosen by the user comes up, the user wins and the game ends. If the number does not come up within three rolls, the computer wins.

+3
Answers (1)
  1. 10 May, 03:02
    0
    The solution code is written in Python 3.

    import random count = 0 flag = False guess = int (input ("Input your guess (2-12) : ")) while (count <=3) : dice1 = random. randint (1, 7) dice2 = random. randint (1, 7) if ((dice1 + dice2) = = guess) : flag = True count + = 1 if (flag) : print ("User wins!") else: print ("Computer wins!")

    Explanation:

    A Random generator is needed for this question and therefore we start by importing Python random class (Line 1)

    Next, create one counter variable, count, to ensure there will be only three rolling of the dices (Line 3). We need another variable, flag, to track the status if the two dices equal to the guess number chosen by user (Line 4).

    Next, prompt use to input a guess number (Line 5).

    Within the while loop, we can use random class method randint () to generate random integer. The arguments 1 and 7 will give one random number ranged from 1 to 6 for dice1 and dice2, respectively (Line 8 - 9).

    If the total of dice1 + dice2 equal to user guess, we turn the flag to True. If not, the flag will remain False after completing entire while loop.

    If the flag turned to True, print the message "User Wins!" else print the message ("Computer wins!")
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Create the logic for a game that simulates rolling two dice by generating two random numbers between 1 and 6 inclusive. The player chooses ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers