In: Computer Science
The program will first prompt the user for a range of numbers. Then the game will generate a random number in that range. The program will then allow the user to guess 3 times. Each time the person takes a guess, if it is not the number then the game should tell them to guess higher or lower. They only get 3 guesses. If they have not guessed the number in three tries then the game should tell them the computer won and thank you for playing and ask them if they would like to play again. If the user wins then the game should congratulate them and ask them if they would like to play again. Each time the program repeats you should store the random number generated into an array. When the program is done the program should display the all of the random numbers back to the user.
Here is the python code to do that
NOTE: If the question is to be done in some other language (c, c++, java) put it in the comments
This is the python code:
# import the random module so that we can use the random function
import random
#the function to play the game
def play():
#ask for 2 numbers
print("Enter 2 numbers:")
a = int(input())
b = int(input())
#use randrange to generate a random number between a and b
randomnumber = a + random.randrange(b-a+1)
chances = 3
#while there are chances keep running this loop
while chances != 0:
print("Guess the number: ")
num = int(input())
if(num == randomnumber):
#if right guess then return the randomnumber
print("Congrats you guessed it right!")
return randomnumber
elif chances == 1:
print("The computer won")
return randomnumber
elif num < randomnumber:
print("Go higher " + str(chances-1) + " chances left")
else:
print("Go lower " + str(chances-1) + " chances left")
chances = chances-1
print("The computer won")
return randomnumber
listofnumbers = []
choice = 'y'
while choice == 'y':
#append the random number to the list
listofnumbers.append(play())
choice = str(input("Do you want to play again?\n"))
print("All the numbers are " + str(listofnumbers))
Here is the output of the above program: