11import random
2-
2+ print ("""
3+ ===================================================================
4+ WELCOME TO THE NUMBER GUESSER GAME!
5+ ===================================================================""" )
36max_range = int (input ("Enter the maximum range for the number guessing game: " ))
47secret_number = random .randint (1 , max_range )
8+ # Flag variable to track if the number has been guessed or not
59number_guessed = False
6-
10+ attempts = 0
711while not number_guessed :
8- guess = int (input (f"Guess a number between 1 and { max_range } : " ))
9- if guess < secret_number :
10- print ("Too low! Try again." )
11- elif guess > secret_number :
12- print ("Too high! Try again." )
13- else :
14- print (f"Congratulations! You've guessed the correct number! It was { secret_number } ." )
15- number_guessed = True
12+ try :
13+ # To make sure the given input is an integer
14+ guess = int (input (f"Guess a number between 1 and { max_range } : " ))
15+ if guess < secret_number :
16+ print ("Too low! Try again." )
17+ attempts += 1 # counts the number of incorrect attempts
18+ elif guess > secret_number :
19+ print ("Too high! Try again." )
20+ attempts += 1 # counts the number of incorrect attempts
21+ else :
22+ print (f"Congratulations! You've guessed the correct number! It was { secret_number } .\n " )
23+ print (f"Number of attempts: { attempts } " )
24+ number_guessed = True #Breaks the loop if the number is guessed correctly
25+
26+ except ValueError : # prevents humanoid error if the user inputs a non-integer value
27+ print ("Invalid input! Please enter a valid integer." )
28+ print ("""
29+ ===================================================================
30+ THANK YOU FOR PLAYING!
31+ ===================================================================""" )
0 commit comments