Python Python Basics Functions and Looping Create a Function

Aly Anwar
PRO
Aly Anwar
Pro Student 235 Points

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
146,440 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.

lemelleio
lemelleio
10,242 Points

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
146,440 Points

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

Aly Anwar
Aly Anwar
Pro Student 235 Points

you deserve the best answer

1 Answer

lemelleio
lemelleio
10,242 Points

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