In: Computer Science
Using pseudocode design a number guessing game program. The program should generate a random number and then ask the user to guess the number. Each time the user enters his or her guess, the program should indicate it was too high or too low. The game is over when the user correctly guesses the number. When the game ends, the program should display the number of guesses that the user made.
Answer: Hello! Kindly find your solution here. If you have any queries, feel free to ask me. Thanks.
Python program-
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1
and 20.')
while guessesTaken < 6:
print('Take a guess.') # There are four spaces in front of print.
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < int(number):
print('Your guess is too low.') # There are eight spaces in front of print.
if guess > int(number):
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job')
if guess != number:
number = str(number)
print('Nope. The number of guesses taken:',guessesTaken)
Pseudocode-
function main()
guessesTaken = 0
output "Hello! What is your name?"
input myname
Set number = random.randint(1, 20)
output"Well,myName, I am thinking of a number between 1
and 20."
while guessesTaken < 6
output "Take a guess."
input guess
Set guessesTaken = guessesTaken + 1
if guess < number then
output"Your guess is too low."
else
if guess > number
then
output"Your guess is too high."
if guess == number then
Set guessesTaken = guessesTaken
output"Good job,correct guess"
End program
if guess != number then
Set number = number
output"Nope. The number of guesses" + guessesTaken
Output-
llo! hat is your ? jah 1. joke, I thinking of aber between 1 and 20. To geas is too lou, 12 You guess is too lon. Take a guess. 19 Your guess is too love. take a gaeas. Lour guess is too low. Take a guess. Your guess is too low. Take a guess. 18 Your guess is too low. ope. The number of guesses taken: 6