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