Python Python Basics Functions and Looping Create a Function

Trish Furstenberg
Trish Furstenberg
3,557 Points

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
203,722 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).

Trish Furstenberg
Trish Furstenberg
3,557 Points

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