Create an algorithm that can be used as a guessing game. Make sure to import random at the start.

print("Welcome to the Guessing Game. A random number will be generated between 1 and 10000, which you will have to guess. Hints will be provided, indicating whether the number is higher or lower than your guess.")import random #As suggested within the task, I have imported randomgame = True # This is a variable to initiate the outermost while loophS = 0 #This calculates the best score. Positioning it here allows it to be recorded without interrupting in-loop calculations
while game == True:
 tries = 0 #Tries will continuously update throughout rn = random.randint(1,10000) #The range is 10000 numbers, which is reasonable for users print("\n\n\n\nRandom number generated") #This is to make the user aware that the game is ready to be played and the user should start guessing while rn == rn:   attempt = int(input("\nWhat is your guess:")) #User adds an integer value. An error will occur if it includes letters  if attempt > rn:   print("Try a lower number")   tries += 1 #This is to ensure the attempt is recorded  elif attempt < rn:   print("Try a higher number") #This ensures hints are provided to users as to the direction of the number generated   tries += 1  elif attempt == rn: #Finally, if the user guesses it, he'll be rewarded with celebratory statements depending on how quickly he guessed   print("\n\nBingo! You got it!")   tries += 1   print("You guessed", tries,"time(s)")   if tries < 3:    print("You're a pro!")   elif tries < 5:    print("Excellent!")   elif tries < 15:    print("Good job!")   else:    print("Try to do a better job next time!")       if hS > tries or hS == 0: #Best score was valued at 0 for simplicity in the beginning. If tries is lower than previous hS, then hS or Best Score is updated to be made smaller, as appropriate.     hS = tries   print("Your best score is:", hS)            anotherTry = input("\nWould you like to play another round? \nType 'y' for yes and anything else for no. \nYour response:") #I made it this way, so as to avoid errors if users intentionally enter invalid input   break
 if anotherTry == "y":  continue # This restarts the outermost loop, i.e. the game is restarted. However, given that Best Score is put outside the loop, the number of tries will not interrupt with the Best Score calculations.  elif anotherTry == "n":  print("\n\n\nThanks for playing the Guessing Game!")  break  break #The second break is needed so the outermost loop is not repeated again

Answered by Sharaf M. Python tutor

2161 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are: (a) even numbers, and (b) from elements in the original list that had even indices .


Write a Python script to take a product name as input and then automatically google search reviews for it and open the top 3 search results in different tabs


Why are negative indexes used?


What is the difference between a float and an integer variable type?


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy