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 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.