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
Moises Miguel
5,028 Pointspython number game
def game():
random_num = random.randint(1, 10)
while True:
try:
guess = int(input("Choose a number 1 - 10: "))
except ValueError:
print("Please only enter numbers ")
else:
if guess == random_num:
print("You are right!, my number was {}".format(random_num))
elif guess < random_num:
print("My number is lower than {}".format(guess))
else guess > random_num:
print("My number is higher than {}".format(guess))
game()
when it runs the terminal says theres a bug, but i can't find it.
[Mod edited code block]
2 Answers
Alx Ki
Python Web Development Techdegree Graduate 14,810 PointsYou also got a mistake with compare operator, because, i guess that it's program saying "my number is higher/lower...."
Here:
elif guess < random_num:
print("My number is lower than {}".format(guess))
Syntax Error:
else guess > random_num:
^
SyntaxError: invalid syntax
Right:
import random
def game():
random_num = random.randint(1, 10)
while True:
try:
guess = int(input("Choose a number 1 - 10: "))
except ValueError:
print("Please only enter numbers ")
else:
if guess == random_num:
print("You are right!, my number was {}".format(random_num))
elif guess > random_num:
print("My number is lower than {}".format(guess))
else:
print("My number is higher than {}".format(guess))
game()
jacinator
11,936 Pointselse guess > random_num: is invalid syntax. Either remove the condition or change else to an elif. The other thing is to double check that you are importing random. You didn't put it in your code block, so I don't know if you imported it into your code.
Moises Miguel
5,028 PointsBut i want the game to say if the number is higher or lower
jacinator
11,936 PointsThere are one of three possibilities.
- guess is equal to random_num
- guess is greater than random_num
- guess is lesser than random_num
If you remove the condition so that it is else: it will take any of the third possibility instances.
If you change else to elif it will continue to function properly. This could be your answer.
If you leave it as it is it will continue to raise a SyntaxError.