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 tried to create my own version of a number game but keep getting sytaxError

This is my code:

    import random
    # Limit number of guesses to 3
    guesses_limit = []

    # generate random number 1-10
    secret_number = random.randit(1, 10)

    while True:
        # get a number guess from player
        guess_number = int(input("Guess a number between 1-10: ")
       # Compare guess to secret number


            #add guess to limit of guesses list
        guesses_limit.append(secret_number)
        if len.guesses_limit < 3: 
            # print hit/miss
            if guess_number > secret_number:
                print("The number is lower! Guess again")
               continue
            elif guess_number < secret_number:
                print("The number is higher! Guess again")
               continue
            else:
                print("You won! The number was {}".format(secret_number))
                break

        elif len.guesses_list > 3:
                print("You lost! My number was {}".format(secret_number))
                break

[MOD: edited code block - srh]

2 Answers

Hi there!

I'll just run through the errors with you if that's okay? First up is

Traceback (most recent call last):
File "main.py", line 15                                                                                         
    guesses_limit.append(secret_number)

Now the line number is a little bit of a trick because it's telling us the line number when it noticed the problem, not where it is, so we look back to the previous line of code on line 10, and we see it's just a missing parenthesis after

guess_number = int(input("Guess a number between 1-10: ")

After that you'll get a couple of indentation errors that I think you'll be able to fix using the error message, finally a couple of confusing error messages:

Traceback (most recent call last):                                                                                
  File "main.py", line 6, in <module>                                                                             
    secret_number = random.randit(1, 10)                                                                          
AttributeError: 'module' object has no attribute 'randit'

This one might not be so clear right away, but again, use the line number - 6, and we see this line:

secret_number = random.randit(1, 10)

All looks good, but python didn't like it, so we look a bit closer and see it's just a small typo on the "randit" part.

Finally you'll see this error:

Traceback (most recent call last):                                                                                
  File "main.py", line 16, in <module>                                                                            
    if len.guesses_limit < 3:                                                                                     
AttributeError: 'builtin_function_or_method' object has no attribute 'guesses_limit' 

This one I'll leave to you. Again, we can use the line number and the message to work out python didn't like:

if len.guesses_limit < 3:

If you get stuck, I recommend looking up the len documentation: https://docs.python.org/3/library/functions.html#len

Hope it helps! :)

What's the error - what line does it say the issue is at?

One thing; this method is called randint():

secret_number = random.randit(1, 10) # wrong method name?

Steve.