0% found this document useful (0 votes)
0 views1 page

Import Random

The document contains a Python script for a number guessing game where the user has to guess a randomly generated number between 1 and 100 within 7 attempts. The game provides feedback on whether the guess is too low, too high, or correct, and handles invalid inputs. If the user fails to guess the number within the allowed attempts, the game reveals the secret number.

Uploaded by

jakerossi2012
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)
0 views1 page

Import Random

The document contains a Python script for a number guessing game where the user has to guess a randomly generated number between 1 and 100 within 7 attempts. The game provides feedback on whether the guess is too low, too high, or correct, and handles invalid inputs. If the user fails to guess the number within the allowed attempts, the game reveals the secret number.

Uploaded by

jakerossi2012
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

import random

def number_guessing_game():
"""Plays a number guessing game with the user."""

lower_bound = 1
upper_bound = 100
secret_number = random.randint(lower_bound, upper_bound)
attempts = 0
max_attempts = 7 # Limit the number of guesses

print(f"Welcome to the Number Guessing Game!")


print(f"I'm thinking of a number between {lower_bound} and {upper_bound}.")
print(f"You have {max_attempts} attempts to guess it.")

while attempts < max_attempts:


try:
guess = int(input("Enter your guess: "))
attempts += 1

if guess < lower_bound or guess > upper_bound:


print(f"Please guess a number within the range ({lower_bound}-
{upper_bound}).")
elif guess < secret_number:
print("Too low! Try again.")
elif guess > secret_number:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number {secret_number} in
{attempts} attempts.")
break # Exit the loop if the guess is correct
except ValueError:
print("Invalid input. Please enter a valid number.")

else: # This 'else' block executes if the loop completes without a 'break'
(i.e., attempts run out)
print(f"Sorry, you ran out of attempts. The secret number was
{secret_number}.")

if __name__ == "__main__":
number_guessing_game()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy