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

I don't know where is my syntax!!

My code is below

answer = randit(1,10)
guess_nubmer=int(input("what's your guess? "))
while True:
  if guess_number > answer:
    print("Your guess is bigger")
    continue
  elif guess_number > answer:
    print("Your guess is smaller")
    continue
  else:
    print("Got it!The answer is exactly {}".format(answer))
    break

when I try to run it on Workspace, this is the result:

File "number_game.py", line 12
break
^

SyntaxError: invalid syntax

I really don't know what is wrong. Can anyone help me?

3 Answers

Steven Parker
Steven Parker
229,608 Points

It's extremely difficult to diagnose unformatted Python.

See the Markdown Cheatsheet below the "Add an Answer" area for instuctions on code formatting. :arrow_heading_down:

But just guessing, your break statement might not be indented correctly. It should line up with the print statement above it, and both should be indented more than the else statement above them.

For the most complete and accurate answers, always show your properly formatted code and also provide a link to the course page.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I'm not sure why it's complaining about break but you definitely have a spelling mismatch (guess_nubmer vs guess_number and you're comparing > both times.

Steven Parker
Steven Parker
229,608 Points

Much better to see with the formatting!

Now that I can see the indentation, break looks okay. But I see a few other things:

  • you wrote "guess_nubmer" instead of "guess_number" when you created the variable (as Kenneth pointed out)
  • you wrote "randit" instead of "randint" on the first line
  • you don't seem to have imported the randint function

Also, what looks like an exclamation point (!) in the last print statement is some fancy Unicode character according to my editor. It could also cause some problems.