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
        guess = int(input("Guess a number between 1 and 10.: "))
    except ValueError:
        print("{} enter a number.".format(guess))
    #continue of there's no error
        #if the guess is correct print success message and end
        if guess == secret_num:
            print("You got it! It was {}".format(secret_num))
        #if the guess is too high, say so    
        elif guess > secret_num:
            print("Too high")
        #if it's too low, say so
        elif guess < secret_num:
            print("Too low")
        #after each run through of the loop add the guess to the array of guesses
play_again = input("Wanna play again? (Y/N)")
if play_again.lower() != "n":

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