DEV Community

Dharani
Dharani

Posted on

Guess The Number Higher or Lower

Introduction

The "Guess the Number (Higher or Lower)" problem is a fun and interactive programming challenge. It helps us understand loops, conditions, and user interaction.


Problem Statement

The program selects a random number, and the user has to guess it.

  • If the guess is too high → show "Too High"
  • If the guess is too low → show "Too Low"
  • If correct → show success message

Approach

  1. Generate a random number
  2. Take input from the user
  3. Compare the guess with the actual number
  4. Give hints (higher or lower)
  5. Repeat until the user guesses correctly

Python Code


python
import random

number = random.randint(1, 100)

while True:
    guess = int(input("Enter your guess (1-100): "))

    if guess < number:
        print("Too Low! Try again.")
    elif guess > number:
        print("Too High! Try again.")
    else:
        print("Congratulations! You guessed correctly.")
        break

## Example:
 Enter your guess : 50
Too Low! Try again.

Enter your guess : 75
Too High! Try again.

Enter your guess: 65
congratulations! you guessed correctly.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)