DEV Community

Arav Mishra
Arav Mishra

Posted on

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
Enter fullscreen mode Exit fullscreen mode

if name == "main":
number_guessing_game()

Top comments (0)