Reputation: 403
Why is Python reporting that the variable perimeter
is undefined?
#The first function will accept the length and width of a rectangle as its two parameters and return the rectangles perimeter
length=float(input("What is the length of your rectangle?"))
width=float(input("What is the width of your rectangle?"))
def rectanglePerimeter(length,width): #the parameter limits , it will accept the length and the width
perimeter= (2*length) + (2*width ) #here we implement the equation for the rectangles perimeter
return perimeter #return the result
def rectangleArea(length,width):
area= length*width
return area
if perimeter> area:
print ("the perimeter is larger than the area")
elif perimeter<area:
print ("the area is larger than the perimeter")
if perimeter == area:
print ("the area and the perimeter are equal")
Upvotes: 0
Views: 467
Reputation: 59974
You haven't called your functions; you have only defined them. To call them, you have to do something like rectanglePerimeter(length, width)
or rectangleArea(length, width)
.
perimeter = rectanglePerimeter(length, width) # Where length and width are our inputs
area = rectangleArea(length, width) # Where once again length and width are our inputs
Also, your if/elif statements seem to be a bit off:
if perimeter > area:
print ("the perimeter is larger than the area")
elif perimeter < area:
print ("the area is larger than the perimeter")
elif perimeter == area: # This should not be nested, and it should be another elif. Infact, you could probably put an else: here.
print ("the area and the perimeter are equal")
Upvotes: 2
Reputation: 27802
2 things:
#The first function will accept the length and width of a rectangle as its two parameters and return the rectangles perimeter
length=float(input("What is the length of your rectangle?"))
width=float(input("What is the width of your rectangle?"))
def rectanglePerimeter(length,width): #the parameter limits , it will accept the length and the width
perimeter= (2*length) + (2*width ) #here we implement the equation for the rectangles perimeter
return perimeter #return the result
def rectangleArea(length,width):
area= length*width
return area
# need to call the functions, and store the results into variables here:
perimeter = rectanglePerimeter(length, width)
area = rectangleArea(length, width)
if perimeter> area:
print ("the perimeter is larger than the area")
elif perimeter<area:
print ("the area is larger than the perimeter")
else: # At this point, perimeter == area
print ("the area and the perimeter are equal")
Upvotes: 1
Reputation: 284786
perimeter
is a local variable, but you're using it outside the function. In fact, you're never actually calling any of your functions.
Upvotes: 0