Untitled
unknown
plain_text
10 months ago
1.1 kB
4
Indexable
import random
def guess_the_number():
print("Welcome to 'Guess the Number'!")
print("I'm thinking of a number between 1 and 100.")
print("You have 7 attempts to guess it correctly.")
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 7
while attempts > 0:
try:
# Get the player's guess
guess = int(input(f"Enter your guess ({attempts} attempts left): "))
# Check the guess
if guess < secret_number:
print("Too low!")
elif guess > secret_number:
print("Too high!")
else:
print(f"Congratulations! You guessed it right. The number was {secret_number}.")
break
# Reduce the number of attempts
attempts -= 1
except ValueError:
print("Please enter a valid number.")
if attempts == 0:
print(f"Sorry, you've run out of attempts. The number was {secret_number}. Better luck next time!")
# Run the game
guess_the_number()
Editor is loading...
Leave a Comment