25.3k views
5 votes
Write a program that plays a reverse guessing game with the user. The user thinks of a number between 1 and 10, and the computer repeatedly tries to guess it by guessing random numbers. It’s fine for the computer to guess the same random number more than once. At the end of the game, the program reports how many guesses it made.

User Geet Mehar
by
5.8k points

1 Answer

0 votes

Answer:

  1. import random
  2. target = 7
  3. count = 0
  4. for i in range(100):
  5. guess = random.randint(1,10)
  6. if(guess == target):
  7. count += 1
  8. print("Total of correct guess: " + str(count))

Step-by-step explanation:

The solution is written in Python 3.

Firstly, import the random module since we are going to simulate the random guess by computer. Next, we presume user set a target number 7 (Line 3). Create a counter variable to track the number of correct guess (Line 4).

Presume the computer will attempt one hundred times of guessing and we use randint to repeatedly generate a random integer between 1 - 10 as guess number (Line 6). If the guess number is equal to the target, increment count by one (Line 8-9).

Display the total number of right guess to terminal (Line 11).

User Mxro
by
5.4k points