Python Question: write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. If the user’s guess is higher that the random number the program should display "too high try again." If the user’s guess is lower than the random number, the program should display "too low,try again" If the user guesses the number the application should congratulate the user and then generate a new random number so the game can start over.

This is what I have so far..

#generates and imports a random number between 1-100
import random
number=random.randint(1,100)

#sets main function
def main():
print("This is a number guessing game.")
print("Guess what number we thought of between 1 and 100")
#print statement

num = number()
#stores number of user guesses
user = 0
#stores number of guesses
guesses = 0
#while statement
while user != num:

user=int(input("Enter your best guess!"))

guesses=guesses +1

if (user > num):
print ("Too high, try again")
else:
print("Congrats, you won!")
print("Restarting game")


main()

Respuesta :

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\ncongratulations.....")

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

       break

   elif guess < answer-20:

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

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

   elif guess > answer+20:

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

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

   else:

       print("incorrect guess\ntry again")

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

Explanation:

Following are the Python program to the given question:

Program Explanation:

  • Import random package.
  • Printing message, and defining "n" variable that uses the random method that select a random value between 1 to 100.
  • In the next step, a while loop is defined that declare a "guess" variable that input value from the user-end.
  • After input value, a conditioanl statement is declared that guesses the random variable value.
  • And in the another block its checks the range value.

Program:

import random#import package

print("This is a number guessing game.")#print message

print("Guess what number we thought of between 1 and 100")#print message

n = random.randrange( 100 ) + 1#defining a variable n that uses random method that holds a value  

while True:#defining a loop to input value and check value

   guess = int(input('Enter your best guess!\n'))#defining a variable guess that inputs value

   if guess == n:#defining if block that check the random number

       print("Congrats, you won!")#print message

       print("Restarting game") #print message

       n = random.randrange( 100 ) + 1 #calling random method that holds a value

   elif guess < n:#defining elif that check random number value less than inputs

       print('too low,try again')#print message

   else:#defining else block

       print('too high try again.')#print message

Output:

Please find the attached file.

Learn more:

brainly.com/question/15018109

Ver imagen codiepienagoya
ACCESS MORE
EDU ACCESS