Exercise 2: Guessing Game With Hints, Attempts and a Quit Option — Possible Solution ==================================================================== import random target = random.randint(1, 100) tries = 0 while True: text = input("Guess a number from 1 to 100 (q to quit): ") if text == "q": print(f"You gave up. The number was {target}") break guess = int(text) tries += 1 if guess == target: print(f"You got it in {tries} tries!") break elif guess < target: print("Too low!") else: print("Too high!") Example run: Guess a number from 1 to 100 (q to quit): 50 Too low! Guess a number from 1 to 100 (q to quit): 75 Too low! Guess a number from 1 to 100 (q to quit): 88 Too low! Guess a number from 1 to 100 (q to quit): 94 Too high! Guess a number from 1 to 100 (q to quit): 91 Too low! Guess a number from 1 to 100 (q to quit): 92 You got it in 6 tries! WHY THIS WORKS AS AN ANSWER ------------------------------ A while True loop has no condition of its own, so every way out has to be a break. Here there are two: the quit check (done before int(), because int('q') would crash) and the correct guess. The counter goes up before the comparison so the winning guess is counted too; if tries += 1 came after the if/elif/else, the winning pass would break out first and the message would be one too low. The sample run always halves the remaining range (50, 75, 88, 94, 91, 92), which is the binary search idea: 100 numbers never need more than 7 guesses.