- Number guessing 1
- Number guessing 2
Python
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
while True:
try:
guess = int(input("Enter your guess: "))
attempts += 1
if guess < 1 or guess > 100:
print("๐ซ Please enter a number between 1 and 100.")
continue
if guess < secret_number:
print("๐ Too low! Try again.")
elif guess > secret_number:
print("๐ Too high! Try again.")
else:
print(f"โ
Congratulations! You guessed it in {attempts} attempts.")
break
except ValueError:
print("โ ๏ธ Please enter a valid number.")
Leave a Reply