Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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!