Guessing Game (Jackpot) โ€” while Loops

A random number, a while loop, and a counter โ€” plus a manual binary-search strategy that turns brute-force guessing into a fast, deliberate hunt.

Overview

This is a Loops exercise. The program picks a random "jackpot" number, and the user keeps guessing until they get it right. It's a practical demonstration of:

Key Concepts

Detailed Explanation

How the program works (step-by-step)

  1. Generate a random number between 1 and 100 and store it in jackpot.
  2. Ask the user for a first guess and store it as an integer in guess.
  3. Initialize counter = 1 (this guess already counts as attempt #1).
  4. Enter a while loop that continues as long as the guess is wrong (guess != jackpot):
    • If guess < jackpot โ†’ print "Guess higher".
    • Else (meaning guess > jackpot, since equality would've exited the loop) โ†’ print "Guess lower".
    • Ask for a new guess and increment the counter.
  5. Once guess == jackpot, the while condition becomes False, so the loop stops.
  6. Print "Sahi Jawab" (Hindi for "Correct Answer") and show the total number of attempts taken.

Why else alone works (without a second elif guess > jackpot)

Since the while loop only continues when guess != jackpot, inside the loop the guess can never be equal to the jackpot. So there are only two real possibilities left: guess < jackpot or guess > jackpot. That's why a plain else safely covers the "guess is too high" case without needing an explicit comparison.

Commands / Syntax

import random

jackpot = random.randint(1, 100)

guess = int(input("Chal guess kar"))
counter = 1

while guess != jackpot:
    if guess < jackpot:
        print("Guess higher")
    else:
        print("Guess lower")

    guess = int(input("Chal guess kar"))
    counter += 1

print("Sahi Jawab")
print("You took", counter, "attempts")

Line-by-line notes

LinePurpose
random.randint(1, 100)Picks the secret number the player must guess
int(input("Chal guess kar"))Reads guess from user; int() is required because input() always returns a string
counter = 1Starts counting from the very first guess
while guess != jackpot:Loop keeps running until the guess matches
if guess < jackpot / elseGives the player a hint to go higher or lower
counter += 1Increments attempt count on every loop pass
print("You took", counter, "attempts")Final summary after the loop ends
Note: print() with comma-separated arguments (e.g. print("You took", counter, "attempts")) automatically inserts spaces between the values โ€” no need to manually convert counter to a string or use string concatenation.

Example Run

Using a binary search strategy (start at the midpoint of the range, then keep halving), the game was solved in this sequence โ€” the actual jackpot turned out to be 58:

AttemptGuessFeedback
130Guess higher
250Guess higher
370Guess lower
460Guess lower
555Guess higher
657Guess higher
759Guess lower
858โœ… Sahi Jawab

Final output:

Sahi Jawab
You took 8 attempts
๐Ÿ’ก Tip: Guessing the midpoint of the current possible range each time (like 1โ€“100 โ†’ 50 โ†’ 75 or 25, etc.) is far more efficient than guessing randomly โ€” this is exactly the idea behind the binary search algorithm, which finds a value in a sorted range in roughly logโ‚‚(n) steps.

Things to Remember

Quick Revision

A while loop repeats until guess != jackpot becomes False. Each pass: compare the guess to the jackpot, print a hint ("Guess higher"/"Guess lower"), take a new guess, and bump a counter. Once correct, the loop exits and prints how many attempts (counter) it took. Guessing via binary search (halving the range each time) solves it in very few tries โ€” in the traced example, 8 attempts to find jackpot = 58.