Untitled
Anonymous
plain_text
03/25/2024 11:05 AM
1016 B
18
Indexable
import random
def play_game():
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100. Can you guess it?")
secret_number = random.randint(1, 100)
attempts = 0
max_attempts = 5
while attempts < max_attempts:
guess = int(input("Enter your guess: "))
attempts += 1
if 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
else:
print(f"Sorry, you've run out of attempts. The number was {secret_number}.")
play_game()
Editor is loading...
Leave a Comment