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.

Respuesta :

Answer:

The solution code is written in Python 3.

  1. import random  
  2. count = 0
  3. flag = False
  4. guess = int(input("Input your guess (2-12): "))
  5. while(count <=3):
  6.    dice1 = random.randint(1, 7)
  7.    dice2 = random.randint(1, 7)
  8.    if((dice1 + dice2) == guess):
  9.        flag = True
  10.    count += 1
  11.    
  12. if(flag):
  13.    print("User wins!")
  14. else:
  15.    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!")