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

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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

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.