Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Max Langs
Max Langs
3,008 Points

Guess won't append to guesses array (for quitting the while loop)

Cant spot the difference between this and the video ... game keeps on running after 5 tries

import random

create game function

def game(): #create random integer secret_num = random.randint(1, 10) #empty array to store guesses guesses = [] #print secret number for cheating print (secret_num)

#run the loop while there are still less than 5 guesses
while len(guesses) < 5:
    #safely get an integer as input
    try:
        guess = int(input("Guess a number between 1 and 10.: "))
    except ValueError:
        print("{} enter a number.".format(guess))
    #continue of there's no error
    else:
        #if the guess is correct print success message and end
        if guess == secret_num:
            print("You got it! It was {}".format(secret_num))
            break
        #if the guess is too high, say so    
        elif guess > secret_num:
            print("Too high")
            continue
        #if it's too low, say so
        elif guess < secret_num:
            print("Too low")
            continue
        #after each run through of the loop add the guess to the array of guesses
        guesses.append(guess)
play_again = input("Wanna play again? (Y/N)")
if play_again.lower() != "n":
    game()
else:
    print("Bye!")

run the game

game()

2 Answers

Max Langs
Max Langs
3,008 Points

There seems to be an issue with the display of the question ...

I am fairly confident the issue is the continue statements. You have to proceed through the else: to get to the append.

See python docs Section 4.4

break, exits the while completely

continue, skips back to the top of the while

So your append is never reached. An easy way to verify this would be to put a print(guesses) after the "guesses.append(guess)" so you can see the contents.