Exercise 7: Guessing Game: Typos and Repeats Do Not Cost an Attempt — Possible Solution ==================================================================== import random target = random.randint(1, 20) tried = [] attempts = 0 while attempts < 5: text = input("Guess (1-20): ") if not text.isdigit() or not 1 <= int(text) <= 20: print("Please enter a whole number from 1 to 20") continue guess = int(text) if guess in tried: print("You already tried that one") continue tried.append(guess) attempts += 1 if guess == target: print(f"You got it in {attempts} attempts!") break print("Too low!" if guess < target else "Too high!") else: print(f"Too bad! The number was {target}") Example run: Guess (1-20): abc Please enter a whole number from 1 to 20 Guess (1-20): 25 Please enter a whole number from 1 to 20 Guess (1-20): 10 Too low! Guess (1-20): 10 You already tried that one Guess (1-20): 15 Too high! Guess (1-20): 13 You got it in 3 attempts! WHY THIS WORKS AS AN ANSWER ------------------------------ The order of the checks is the point. Validation comes first and uses 'or' so that int() is only called on text that is already all digits (a not-digits test that is True short-circuits the rest). The repeat check comes next. Only after both has the guess earned an attempt, so attempts += 1 sits below them; if it were at the top of the loop, every typo and every repeat would be charged. The while loop's else runs when the condition attempts < 5 becomes false without a break, which means the five attempts were used up.