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

Maor Beeri
Maor Beeri
2,154 Points

I get a syntax error to my else statement that points to print("my number is less than {}".format(guess))

import random

def game(): secret_num = random.randint(1,10) guesses = []

while len(guesses) < 5:

    try:
        guess = int(input("Guess a number between 1 and 10: "))

    except ValueError:
        print("{} is not a number".format(guess))

    else:

        if guess == secret_num:

            print("Yay you are correct, my number was {}".format(secret_num))
            break

        elif guess < secret_num:

            print("my number is greater than {}".format(guess)

        else:

            print("my number is less than {}".format(guess))

        guesses.append(guess)

else:
    print("you didn't get it my number was {}
          ".format(secret_num))

    play_again = input("Do you want to play again? Y/n")

    if play_again.lower() != n:
        game()  
    else:
        print(" Good Bye!)

game()

2 Answers

Steven Parker
Steven Parker
229,744 Points

The actual issue is a few lines above.

This print statement is missing a closing parenthesis:

            print("my number is greater than {}".format(guess)

Also, the string in this statement is missing a closing quote:

        print(" Good Bye!)
Maor Beeri
Maor Beeri
2,154 Points

thank you very much! although I still get an error in this line: play_again = input("Do you want to play again? Y/n")

(by the way, how do you copied my code lines in such a nice way? :) )

Steven Parker
Steven Parker
229,744 Points

You may also need to reconstruct the string that appears split above into a single line:

        print("you didn't get it my number was {}".format(secret_num))

And use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:
Or watch this video on code formatting.

Maor Beeri
Maor Beeri
2,154 Points

sorry but I get indentation error here:

play_again = input("Do you want to play again? Y/n")

what's the cause of this

Steven Parker
Steven Parker
229,744 Points

The code you see above has not been formatted to display correctly using Markdown, so you're not seeing the actual indentation.