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

Can someone help me with my code?

I tried writing this code out by myself before the guy does it on his video, however when i try running the game it showing a SyntaxError for the line elif guess < secret_num:

can someone please have a look at my code and see where i've gone wrong.

import random

guesses_remaining = 5
secret_num = random.randint(1, 10)

def play_game():    
    while guesses_remaining > 0 :
        # get a number guess from the player
        guess = int(input("Guess a number between 1 and 10: "))
        # compare guess to secret number
        if guess == secret_num:
            print("You got it! My number was {}.".format(secret_num))
            play_again = input("Write YES to play again! ")
            if play_again == 'YES':
                play_game()
            else:
                print("Thanks for playing")
        elif guess > secret_num:        
            print("A Little bit lower")
            guesses_remaining -= 1
            print("You have {} guesses remaining".format(guesses_remaining)
        elif guess < secret_num:
            print("A little higher and you got it!")
            guesses_remaining -= 1
            print("You have {} guesses remaining".format(guesses_remaining)
        if guesses_remaining < 0:
                  print("Sorry you lose!!")
                  play_again = input("Write YES to play again! ")
            if play_again == 'YES':
                play_game()
            else:
                print("Thanks for playing")


play_game()                  

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! In the print lines where you're using the .format you've forgotten a closed parenthesis on both of them. There should be one more closed parenthesis after (guesses_remaining).

Hope this helps! :sparkles:

:bulb: Extra tip: If a compiler is telling you that a problem is on a specific line, and you're 100% sure that the line is correct, start looking upwards in your code. Nine times out of ten it'll be on a line or two before that. The reason it's saying there's a problem there is because the line above it is actually where the problem starts. It's expecting a closed parenthesis and instead got an elif.

It does! however i am now getting an IndentationError for line:

if play_again == 'YES':

it says unindent does not match outer indentation level.

Jennifer Nordell
Jennifer Nordell
Treehouse Staff

You're right! But keep my tip in mind. Look at the 2 lines above that one. They are not indented correctly. They should be in line with where your if play_again == 'YES': is :smiley:

They have been indented not once, but twice which puts them too far to the right.