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'm getting an error for having a line of code out of a loop.

I was making a number game when I got an error for having code outside of the loop, while Python was expecting it to be. How do I make it so I can use the code without causing the error? Here's my code:

import random

sn = random.randint(1, 100)
ga = []

def game():
    global sn
    global pa
    global g
    while len(ga) < 15:
        g = input("""Guess a number between 1 and 100:
  """)
        if g.isdigit() == False:
            print("{} is not a number!".format(g))
        if g == sn:
            pa = str(input("""You guessed my number! My number was {}. Play again? Y/N:
    """.format(sn)))
            if pa == "y":
                sn = random.randint(1, 100)
                continue
            elif pa == "n":
                print("Thanks for playing! Hope to see you again!")
                break
            else:
                print("I'll take that as a no. Thanks for playing anyway!")
                break
        try:
            if g > sn:
                print("That's not my number! My number is smaller than that!")
            elif g < sn:
                print("That's not my number! My number is larger than that.")
            ga.append("a")
            continue
game()

What am I supposed to do to fix this?

Thanks! I was having to use the try command because if somebody put in a string, it would try to compare an integer to a string, which wouldn't work. After that, I just removed the whole try command and decided to figure out why it was crashing that. It was the exact same reason I put it there; to stop it from crashing from comparing an integer to a string. I just had to find a method that would make it so that when somebody typed in a string, it would say that it wasn't a number. Sorry for the misunderstanding of code though, that was on my side. I didn't exactly explain what I was trying to do, I just said what problem I was having. I was trying to stop the crash from happening when you typed in a string. Thanks again, I've been struggling for days to figure out what to do to fix it.

1 Answer

with any try block, you need to have an except clause. What kind of exception do you expect to raise? You're only doing comparisons between 2 objects and then attempting a list operation.

If I don't know what to expect I'll sometimes do like a

try:
    some_logic
except:
    pass

and just keep the programming going and let exceptions silently fail.