211k views
0 votes
Write a program that plays a guessing game with the user. Specifically, your program should randomly pick a number between 1 and 100. Then, ask the user for a guess. You should detect and tell the user if the guess is not a valid guess. Otherwise, tell the user their guess was too high or too low. The program should continue to prompt the user for new guesses until they get the correct number, telling them each time if the guess was too high or too low or invalid.

You have been supplied code to pick a random number between 1 and 100 each time you run your program. Here are a couple development/debugging strategies for this "target" variable:

Print out the random number, to make sure your program is acting correctly – remember to remove/comment this before running unit tests/submitting.

Temporarily set the random "seed" to a value, which will have the effect of always choosing the same random number – the unit tests have fixed seeds that you can use with known outcomes.

Temporarily set the "target" variable to a fixed number, so you can test to see how your program responds in different testing situations.

Here’s a sample run of a working version of the program:

Enter your guess (between 1 and 100): 50

Too high!

Enter your guess (between 1 and 100): 0

Invalid guess, try again!

Enter your guess (between 1 and 100): 101

Invalid guess, try again!

Enter your guess (between 1 and 100): 25

Too high!

Enter your guess (between 1 and 100): 12

Too high!

Enter your guess (between 1 and 100): 6

Too high!

Enter your guess (between 1 and 100): 3

Too low!

Enter your guess (between 1 and 100): 4

Too low!

Enter your guess (between 1 and 100): 5

You win!

User Woto
by
5.4k points

1 Answer

0 votes

Answer:

import random

arr=[]

for i in range(100):

arr.append(i)

while True:

answer=random.choice(arr)

guess=int(input("enter your guess number between 0-100: "))

if answer is guess:

print("right guess\\congratulations.....")

print("the answer was: "+str(answer))

break

elif guess < answer-20:

print("you guessed too low....\\try again")

print("the answer was: "+str(answer))

elif guess > answer+20:

print("you guessed too high....\\try again")

print("the answer was: "+str(answer))

else:

print("incorrect guess\\try again")

print("the answer was: "+str(answer))

Step-by-step explanation:

the program is in python programming language

User Avneesh Agrawal
by
5.4k points