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

Need Help Understanding the Error in this Function

I have been stuck on this exercise for a couple of days and just really want to move on. I keep tinkering with this function, and I truly cannot understand why this isn't working! I keep getting an EOF error, which has something to do with redirecting input back in to the function, I guess. Can anyone please help me understand why this isn't working?

def square (number): return number * number

number = int(input("What is the number? ")) print("The square of your number is {}".format(square))

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

number = int(input("What is the number?  "))
print("The square of your number is {}".format(square))


So, no matter what input number I used when I run the function, the response I get back is exactly this:
The square of your number is <function square at 0x7f7fade45e18>  

I am stumped....

5 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Well you defined the function like:

def square (number):
    return number * number

It is expecting 1 parameter - you call it number

So, when you call your function - you need to pass a parameter like:

print("The square of your number is {}".format(square(number)))

You should probably review the video again regarding defining and calling functions

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Try passing number to the square() function

It......does....make sense. But the abstraction is melting my brain right now. I need to get in some more practice! I really appreciate you taking the time to help me understand that piece!

Hmm, ok, so how would that look in practice?

Ok, great, that did work! I guess I will need to review the video (already rewatched the last 2 on functions about 5 times each!)

What confuses me, is that I thought I had asked it to return the square of my input in the function, where I asked "square" to return number*number (my input number multiplied by itself.)

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Yes, I can see your confusion. Your function accepts 1 parameter and then returns the square (number * number).

You're input and print is outside the function (hence the purpose of functions being reusable).

So, once you have the number you wish to have squared - you call your function to square it. So you need to pass the argument so the function knows what value to do it's stuff on.

Also, that's why function calls always have the parenthesis like square(). There can be 0 to many parameters defined for a function.

Hope that clears it up a bit