user2548833
user2548833

Reputation: 403

Python — why is perimeter undefined?

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

Answers (3)

TerryA
TerryA

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

jh314
jh314

Reputation: 27802

2 things:

  1. You should store the results of the function calls into a variable.
  2. You can replace the last if statement with an else (since the third case only ever happens when they are equal)
#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

Matthew Flaschen
Matthew Flaschen

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

Related Questions