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

Create a Function python.....

What have I done wrong now??

squaring.py
def square(number)
return square * 2

2 Answers

Hi Katrine Guirguis , a quick couple of fixes:

First, make sure when you're defining a function you end the definition statement with a colon ":" after the parenthesis:

    def square(number):

Second, make sure that whatever parameter you assign in the arguments, you actually use that parameter in your return

    def square(number):
        return number * 2

Third, indentation is key. reference my example above to see how the return is nested.

Finally, a square number is a number multiplied to itself. so 6 * 6, 7 * 7, not 7 * 2. I would make sure your number multiplies itself:

    def square(number):
        return number * number

or

    def square(number):
        return number ** 2

I hope that helps!

so i got as far as you explained but now i can figure out how to take the output and assign it to a variable called result... i tried the follow ing but it didnt work

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

square(3)

Jose Alvarez you would assign the result to a variable named response like so:

def square(number):
    # code inside here that does stuff

# this is how you would set response to be the return of the function
response = square(3)

when the function finishes its work and returns the value, that value will then be set to response