Untitled
unknown
plain_text
a year ago
1.1 kB
5
Indexable
import random
def number_guessing_game():
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 10
print(f"You have {max_attempts} attempts to guess the number.")
while attempts < max_attempts:
try:
guess = int(input("Enter your guess: "))
attempts += 1
if guess < secret_number:
print("Too low!")
elif guess > secret_number:
print("Too high!")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
break
except ValueError:
print("Please enter a valid number.")
if attempts == max_attempts and guess != secret_number:
print(f"Sorry, you've used all your attempts. The number was {secret_number}. Better luck next time!")
if __name__ == "__main__":
number_guessing_game()Editor is loading...
Leave a Comment