193k views
16 votes
A computer game picks a random number between 1 and 100, and asks the player to guess what the number is. If the player makes a correct guess, the game congratulates them, and stops. If the player does not make a correct guess, the game tells the player that their guess is either too high, or too low. If the player guesses with an answer that is not a number between 1 and 100, an error message is displayed. The game continues to give the player chances to guess, until the player guesses correctly. You are going to plan an interactive program to implement this game. do this using scratch

1 Answer

6 votes

Answer:

In Python:

import random

computerguess = random.randint(1,101)

userguess = int(input("Take a guess: "))

while not (userguess == computerguess):

if userguess<1 or userguess>100:

print("Error")

elif not (userguess == computerguess):

if userguess < computerguess:

print("Too small")

else:

print("Too large")

userguess = int(input("Take a guess: "))

print("Congratulations")

Step-by-step explanation:

This imports the random module

import random

Here, a random number is generated

computerguess = random.randint(1,101)

This prompts the user for a guess

userguess = int(input("Take a guess: "))

The following loop is repeated until the user guess correctly

while not (userguess == computerguess):

If user guess is not between 1 and 100 (inclusive)

if userguess<1 or userguess>100:

This prints error

print("Error")

If the user guessed is not correct

elif not (userguess == computerguess):

If the user guess is less

if userguess < computerguess:

It prints too small

print("Too small")

If otherwise

else:

It prints too large

print("Too large")

This prompts the user for a guess

userguess = int(input("Take a guess: "))

The loop ends here

This prints congratulations when the user guess correctly

print("Congratulations")

User Hkachhia
by
5.1k points