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 trialAditya Agarwal
Courses Plus Student 564 PointsWhy syntax error?
import random
secret=random.randint(1, 10)
while True: gn=int(input("Guess the number ") if gn == secret: print("U guessed it right, the number is {}".format(sn)) break else: print("Try again")
if gn == secret:
^
SyntaxError: invalid syntax
2 Answers
Rohan Ubhare
6,077 PointsTry
import random
secret=random.randint(1, 10)
while True: gn=int(input("Guess the number ") if gn == secret: print("U guessed it right, the number is {}".format(secret)) break else: print("Try again")
Try replacing .format(sn) in your code to .format(secret) since I don't see anything assigned to 'sn'
Josh Roberts
9,930 PointsI found 2 errors in your code. rohanubhare found the first with needing to use 'secret' in place of 'sn', since you had not set 'sn' to anything previously. The second was the line, gn = int(input("Guess the number "), is missing the closing ")". The code below worked when I tested it, with those 2 changes.
import random
secret = random.randint(1, 10)
while True:
print(secret)
gn = int(input("Guess the number "))
if gn == secret:
print("U guessed it right, the number is {}".format(secret))
break
else:
print("Try again")