Number Guessing Game
Number Guessing Game
def number_guessing_game():
lower_bound = 1
upper_bound = 100
#Initialize variables
number_of_guesses = 0
guessed_correctly = False
if difficulty == '1':
max_attempts = None #No limit on guesses for Easy
elif difficulty == '2':
max_attempts = 10 #10 guesses for Medium
elif difficulty == '3':
max_attempts = 5 #5 guesses for Hard
else:
print ("Invalid choice, setting to Easy mode by default.")
max_attempts = None
#Check if the player has run out of attempts (for Medium and Hard
levels)
if user_guess == number_to_guess:
guessed_correctly = True
print (f"\nCongratulations!You guessed the correct number
{number_to_guess} in {number_of_guesses} attempts.")
update_high_score (number_of_guesses)
elif user_guess < number_to_guess:
print ("Too low!Try again.")
else:
print ("Too high!Try again.")
except ValueError:
def update_high_score(current_score):
try:
with open("high_score.txt", "r") as file:
high_score = int(file.read())
except FileNotFoundError:
high_score = None
number_guessing_game()