Answer:
The solution code is written in Python:
- import random
- target = int(input("Enter the target sum to roll for: "))
-
- die1 = random.randint(1,6)
- die2 = random.randint(1,6)
- print("Roll: " + str(die1) + " and " + str(die2) + ", sum is " + str(die1 + die2))
- count = 1
-
- while(die1 + die2 != target):
- die1 = random.randint(1,6)
- die2 = random.randint(1,6)
- print("Roll: " + str(die1) + " and " + str(die2) + ", sum is " + str(die1 + die2))
- count += 1
-
- print("Got it in " + str(count) + " rolls!")
Step-by-step explanation:
Since we need to generate random value for die, we import random module (Line 1).
Next, we use input function to prompt user to input a target number (Line 2).
We use randint method in the random module to generate a random integer between 1 - 6 (Line 4-5).
Display the die numbers and the sum (Line 6)
Create one counter variable track the number of rolling and initialize it with one (Line 7).
Create a while loop and set a condition that while the sum of two die numbers are not equal to the target the loop should keep going (Line 9)
In the loop generate two new random die numbers again and display the generated die numbers and the sum (Line 10 - 12). Increment count by one and proceed to the next iteration (Line 13).
At last display the message number of rolls after finishing the loop (Line 15).