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.
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:
while loop (loop that runs until a condition becomes false)if / else branching inside a looprandom.randint(a, b) โ generates a random integer between a and b (inclusive).while <condition>: โ repeats the block as long as the condition is True. Here it keeps running as long as guess != jackpot.int(input(...)) โ takes user input (which is always a string) and converts it to an integer so it can be compared numerically.counter) initialized before the loop and incremented (counter += 1) on every iteration, used to count how many times something happened.jackpot.guess.counter = 1 (this guess already counts as attempt #1).while loop that continues as long as the guess is wrong (guess != jackpot):
guess < jackpot โ print "Guess higher".guess > jackpot, since equality would've exited the loop) โ print "Guess lower".guess == jackpot, the while condition becomes False, so the loop stops."Sahi Jawab" (Hindi for "Correct Answer") and show the total number of attempts taken.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.
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 | Purpose |
|---|---|
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 = 1 | Starts counting from the very first guess |
while guess != jackpot: | Loop keeps running until the guess matches |
if guess < jackpot / else | Gives the player a hint to go higher or lower |
counter += 1 | Increments 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 convertcounterto a string or use string concatenation.
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:
| Attempt | Guess | Feedback |
|---|---|---|
| 1 | 30 | Guess higher |
| 2 | 50 | Guess higher |
| 3 | 70 | Guess lower |
| 4 | 60 | Guess lower |
| 5 | 55 | Guess higher |
| 6 | 57 | Guess higher |
| 7 | 59 | Guess lower |
| 8 | 58 | โ 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.
input() in int() (or float()) when you need to do numeric comparisons โ raw input() output is a string.while loop needs some line inside it that eventually makes the condition False (here, getting new input) โ otherwise it becomes an infinite loop.if / else is enough โ you don't always need elif.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.