Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping Create a Function

Katrine Guirguis
Katrine Guirguis
2,587 Points

Create a Function python.....

What have I done wrong now??

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


    def square(number):
        return number ** 2

I hope that helps!

Jose Alvarez
Jose Alvarez
382 Points

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)


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