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

Stuck on this task!

What is wrong with my code? The error says 'number' is undefined - I can't figure it out! I've tried many variations

squaring.py
def square(number):
    number = float(input(" Please Enter any numeric Value : "))
    square = number * number
print("The Square of a Given Number {0}  = {1}".format(number, square))

2 Answers

Steven Parker
Steven Parker
216,136 Points

It looks like you're working too hard! Here's a few hints:

  • the instructions don't ask you to "input" anything, your method should use the passed-in parameter
  • the instructions don't ask you to "print" anything
  • the instructions do ask you to return the result value

And the error you were seeing was because the "print" statement was not indented, and therefore not part of the function; and outside the function "number" is not defined (and "square" is the function itself).

Right! I've been trying so many combinations that I forgot the 'return' in my code when I posted this. I've amended it to this, but I'm getting an EOF error on line 2 and I can't figure it out: .

Ran 1 test in 0.000s

OK

E

ERROR: test_results (main.TestFunctionDefinitionExecution)

Traceback (most recent call last): File "", line 52, in test_results File "", line 2, in square EOFError: EOF when reading a line


Ran 1 test in 0.000s

FAILED (errors=1)


UPDATED CODE:

def square(number): number = int(input("Enter a number to find its square: ")) return number * number