0% found this document useful (0 votes)
21 views2 pages

Number Guessing Game

The document contains a Python script for a number guessing game where players guess a randomly generated number between 1 and 100. Players can choose from three difficulty levels that determine the number of attempts allowed. The game tracks the number of guesses and updates a high score if the player achieves a better score than previously recorded.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Number Guessing Game

The document contains a Python script for a number guessing game where players guess a randomly generated number between 1 and 100. Players can choose from three difficulty levels that determine the number of attempts allowed. The game tracks the number of guesses and updates a high score if the player achieves a better score than previously recorded.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import random

def number_guessing_game():

#Set the range for the random number

lower_bound = 1
upper_bound = 100

#Generate a random number between the bounds

number_to_guess = random.randint(lower_bound, upper_bound)

#Initialize variables

number_of_guesses = 0
guessed_correctly = False

print ("Welcome to the number guessing game!")

#Choose difficulty level

print ("Choose a difficulty level:")


print ("1. Easy (Unlimited attempts)")
print ("2. Medium (10 attempts)")
print ("3. Hard (5 attempts)")

difficulty = input("Enter the difficulty level (1, 2 or 3): ")

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

print (f"\nI'm thinking of a number between {lower_bound} and {upper_bound}.Can


you guess what it is?")

#Start the guessing loop

while not guessed_correctly:


try:

#Check if the player has run out of attempts (for Medium and Hard
levels)

if max_attempts is not None and number_of_guesses >= max_attempts:


print (f"\nSorry!You've run out of attempts.The correct number was
{number_to_guess}.")
break

#Take input from the user

user_guess = int(input("Enter your guess: "))


number_of_guesses = number_of_guesses + 1
#Check if the guess is correct

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

#If using attempts limit, show remaining attempts

if max_attempts is not None:


print (f"Remaining attempts: {max_attempts - number_of_guesses}")

except ValueError:

#Handle invalid input

print ("Please enter a valid integer.")

#Ask if the user wants to play again

play_again = input("\nDo you want to play again? (yes/no) ").lower()


if play_again == 'yes':
number_guessing_game()
else:
print ("Thanks for playing!Goodbye.")

def update_high_score(current_score):

#Load the high score from a file

try:
with open("high_score.txt", "r") as file:
high_score = int(file.read())

except FileNotFoundError:
high_score = None

#Update the high score if the current score is better

if high_score is None or current_score < high_score:


with open("high_score.txt", "w") as file:
file.write(str(current_score))
print (f"New high score!You set a record with {current_score} attempts.")
else:
print (f"Current high score: {high_score} attempts.")

#Start the game

number_guessing_game()

You might also like