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 trialTrish Furstenberg
3,557 PointsStuck 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
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
231,268 PointsIt 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
3,557 PointsRight! 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