0% found this document useful (0 votes)
4 views

Arav's python games

This document contains a Python script for a number guessing game where the player has 15 attempts to guess a randomly generated secret number between 1 and 1000. The script includes functions to get user input, validate guesses, and provide feedback on whether the guess is too low, too high, or correct. If the player fails to guess the number within the allowed attempts, the game reveals the secret number.

Uploaded by

bhakar.arav
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)
4 views

Arav's python games

This document contains a Python script for a number guessing game where the player has 15 attempts to guess a randomly generated secret number between 1 and 1000. The script includes functions to get user input, validate guesses, and provide feedback on whether the guess is too low, too high, or correct. If the player fails to guess the number within the allowed attempts, the game reveals the secret number.

Uploaded by

bhakar.arav
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/ 1

# main.

py
import random

# define range and max_attempts


lower_bound = 1
upper_bound = 1000
max_attempts = 15

# generate the secret number


secret_number = random.randint(lower_bound, upper_bound)

# Get the user's guess


def get_guess():
while True:
try:
guess = int(input(f"Guess a number between {lower_bound} and
{upper_bound}: "))
if lower_bound <= guess <= upper_bound:
return guess
else:
print("Invalid input. Please enter a number within the specified
range.")
except ValueError:
print("Invalid input. Please enter a valid number.")

# Validate guess
def check_guess(guess, secret_number):
if guess == secret_number:
return "Correct Answer Great Job, Here is a Cookie🍪"
elif guess < secret_number:
return "Too low, Sorry"
else:
return "Too high, Sorry"

# track the number of attempts, detect if the game is over


def play_game():
attempts = 0
won = False

while attempts < max_attempts:


attempts += 1
guess = get_guess()
result = check_guess(guess, secret_number)

if result == "Correct":
print(f"Congratulations! You guessed the secret number {secret_number}
in {attempts} attempts.")
won = True
break
else:
print(f"{result}. Try again!")

if not won:
print(f"Sorry, you ran out of attempts! The secret number is
{secret_number}.")

if __name__ == "__main__":
print("Welcome to the Number Guessing Game!")
play_game()

You might also like