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!

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


Why isn't my number_game2.py not working?

import random

def game(): secret_num = int(input("Enter a number 1 to 10 for the computer to guess")

num_guess = []

while len(num_guess) < 5: try: x = 1 y = 10 guess = random.randint(x,y): print(guess) if guess == secret_num: print("The computer guessed {} and it was right!".format(secret_num)) else: user_input = input("Was the computer guess too high or low? Type 'low' or 'high'")
if user_input.lower == "low": x = guess num_guess.append(guess)
elif user_input.lower == "high": y = guess num_guess.append(guess) else: print("That is not one of the options!")
play_again = input("Do you wish to play again? Y/n")
if play_again.lower != "n": game()
else: break


        File "number_game2.py", line 6                                            
num_guess = []                                                          

SyntaxError: invalid syntax

Nicholas Anstis
Nicholas Anstis
2,095 Points

If your code were a little bit cleaner :P Maybe we could help you out but i can't see the error in this mess

2 Answers

Steven Parker
Steven Parker
227,167 Points

Indentation is critical in Python, so it's important to blockquote your code properly. Even better, make a snapshot of your workspace and post the link to it here.

However, even as-is, it looks like you are missing a closing parenthesis on this line:

    secret_num = int(input("Enter a number 1 to 10 for the computer to guess")  # <- add another ")" here

Also, there seems to be a stray colon on this line:

            guess = random.randint(x,y):  # <- remove the ":" here

And there are a couple of places where you use lower but forgot the parentheses () after it.

Plus there doesn't seem to be an "except" to go with the "try"

Hopefully those hints will get you going.


now it's saying how there is an unexpected indent on line 26. Problem for me is that on line 26: it's an else statement for the while loop. Also, I used indentation view to make sure it was properly aligned. Any help would be greatly appreciated