Python Python Basics (2015) Number Game App Number Game Takeaways

Inverse Number Game - Ending the while loop after the 3rd Guess

I can't figure out how to not display the "higher or lower" question after the 3rd guess. This question is unnecessary after the 3rd guess because there will not be a 4th guess to apply this knowledge.

guesses = []
range_low = 1
range_high = 10

while len(guesses) < 3:  
    while True:  
        guess = random.randint(range_low,range_high)
        if guess in guesses:
            continue
        else:
            break   
    guesses.append(guess)
    guess_yes_no = input("Is your secret number {}? Y/n: ".format(guess))
    if guess_yes_no == "y":
        print("Wow! I got it! I'm a genius!")
        break
    else:
        guess_feedback = input("Higher or lower?: ")
        if guess_feedback == "higher":
            range_low = guess
        else:
            range_high = guess

1 Answer

Jeffrey Duarte
Jeffrey Duarte
9,706 Points

Hi Jim,

You want to change your if-else statement to an if-elif that checks the number of guesses again before asking the follow up question. Although, you are running a check for number of guesses when you start your while loop, you must check it again in your if-else statement, because you are appending the current guess in the middle of the loop and so it isn't accounted for until the loop starts up again.

Your if-else statement should be adjusted to read as follows:

if guess_yes_no == "y":
    print("Wow! I got it! I'm a genius!")
    break
elif len(guesses) < 3:
    guess_feedback = input("Higher or lower?: ")
    if guess_feedback == "higher":
        range_low = guess
    else:
        range_high = guess

I hope this helps.

Yes, of course! Thank you!