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

anyone help please

im stumped i have been trying this for 30 mins

squaring.py
number = 3


def square(total):
    total = number ** 2
    return total



result = total
Steven Parker
Steven Parker
231,248 Points

Great response lemelleio, you should post it as an answer instead of a comment.
If I was a moderator, I'd have promoted it for you.

Thank you Steven Parker I didn't even notice it was just a comment. You've actually helped me out a ton when I find your answers to challenges I'm stuck on. Happy to pay it forward!

Steven Parker
Steven Parker
231,248 Points

Now I can upvote you, and Aly can give you a "best answer" if he chooses.

you deserve the best answer

1 Answer

Hey Aly

Your code is almost right:

number = 3 
#setting the initial value is not necessary, 
#the code challenge will pass its own value into your function,
#in order to test your code.

def square(total):  
    total = number ** 2
    return total
# the issue with your function is the test passes in 5(for example) as total, 
#you then reassign total(5) to a new the value of the variable "number", to the power of 2. 
#well the variable "number" has no value, as you did not assign it any. 
#so its value is NULL, you then return the reassigned total (which is now NULL).


result = total
#no need to assign a new variable here, 
#the test only wants you to return a squared number of the parameter passed into the function.


#example solution
def square(number):
    return number ** 2