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 Python Basics Functions and Looping Create a Function

Code Challenge - squaring

Hey, I've come up with the below for "Create a function named square. It should define a single parameter named number.":

def square(number) : return square square = number * number result = square (3) print (square)

But I'm getting "UnboundLocalError: local variable 'square' referenced before assignment". I have no idea where I'm going wrong and I'm struggling with this bit of code

Background, never coded before

 def square(number) :
        return square
        square = number * number
        result = square (3)
print (square)
Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Joshua, it's not cheeky at all. I am a beginner myself and understand your struggle!

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Joshua, the return statement should be placed at the end of the method definition. Otherwise, it will not return the square product. You can square the number parameter inside a return statement. It looks cleaner and saves you a line of code. And you don't need a print statement. Just call the square function outside the method declaration and give it an integer as the parameter.

def square(number):
    return number * number # you can square the parameter inside a return statement

result = square(3) # call the function and give it a number that is stored in the result variable

# this is how it looks like under the hood

# you call square(3)
# inside the square function 3 is the number that you defined as the method's parameter
# the square function takes 3 and the return statement looks like: return 3*3 and returns 9 
# so 9 can be stored in the result

Makes sense? Please don't hesitate to ask more question until the topic is understood.

Thank you so much - I was trying to over complicate things! Keep it simple!

Hey, I'll be honest, still going over my head. I'm struggling with basic terms, let alone anything relatively complex!

Can I be cheeky and ask you to write the answer so I can work through it in reverse?

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

:smile: Just keep on coding and things resolve ...

Jessika Griffin
Jessika Griffin
376 Points

Feeling the same way on this one haha. It's been quite a while since I've used my brain in this way and seeing a concrete example would be helpful. There might be some examples elsewhere online?

Edit - oh, turns out I just didn't realize I needed the colon