Number game

import random def number_guessing_game(): """Plays a number guessing game with the user.""" number = random.randint(1, 100) attempts = 0 print("Welcome to the Number Guessing Game!") print("I'm thinking of a number between 1 and 100.") while True: try: guess = int(input("Enter your guess: ")) except ValueError: print("Invalid input. Please enter a number.") continue attempts += 1 if guess < number: print("Too low!") elif guess > number: print("Too high!") else: print(f"Congratulations! You guessed the number {number} in {attempts} attempts.") break if name == "main": number_guessing_game()

Jan 20, 2025 - 22:37
 0
Number game

import random

def number_guessing_game():
"""Plays a number guessing game with the user."""

number = random.randint(1, 100)
attempts = 0

print("Welcome to the Number Guessing Game!")
print("I'm thinking of a number between 1 and 100.")

while True:
    try:
        guess = int(input("Enter your guess: "))
    except ValueError:
        print("Invalid input. Please enter a number.")
        continue

    attempts += 1

    if guess < number:
        print("Too low!")
    elif guess > number:
        print("Too high!")
    else:
        print(f"Congratulations! You guessed the number {number} in {attempts} attempts.")
        break

if name == "main":
number_guessing_game()

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow