Untitled
unknown
plain_text
a year ago
903 B
4
Indexable
import random
# Function to play the guessing game
def guess_number():
# Random number between 1 and 100
number_to_guess = random.randint(1, 100)
attempts = 0
print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")
# Game loop
while True:
# Ask for user input
try:
guess = int(input("Enter your guess: "))
except ValueError:
print("Please enter a valid number.")
continue
attempts += 1
# Check if the guess is too high, too low, or correct
if guess < number_to_guess:
print("Too low! Try again.")
elif guess > number_to_guess:
print("Too high! Try again.")
else:
print(f"Congratulations! You've guessed the number in {attempts} attempts.")
break
# Start the game
guess_number()
Editor is loading...
Leave a Comment