Sarah Hyland
Sarah Hyland

Reputation: 267

Returning two values with arguments in the function

### The formulas for the area and perimeter.
def area(a, b, c):
    # calculate the sides
    s = (a + b + c) / 2
    # calculate the area
    areaValue = (s*(s-a)*(s-b)*(s-c)) ** 0.5
    # returning the output

    # Calculate the perimeter
    perimValue = a + b + c
    # returning the output.

    return areaValue,perimValue

areaV, perimeterV = area(a, b, c)

### The main function for the prompts and output.
def main():
    # The prompts. 
    a = int(input('Enter first side: '))
    b = int(input('Enter second side: '))
    c = int(input('Enter third side: '))

    # The output statements. 
    print ("The area is:", format(areaV(a, b, c),',.1f'),"and the perimeter is:", format(perimeterV(a, b, c), ',.1f'))

### Calling msin
main()

I am trying to return the two values from the area function but when I try to do so, I get an error saying the a b and c is not defined when I try to call the function.

Note: My instructor has told us that the area and the perimeter need to be calculated in only 1 function. They can't be separated.

Is there a way I can stop that error from happening?

Upvotes: 2

Views: 80

Answers (2)

Hackaholic
Hackaholic

Reputation: 19733

you need to put

areaV, perimeterV = area(a, b, c) 

in the main after user input. Because a,b,c is defined in the scope of main function

it should be like this:

def main():
    # The prompts. 
    a = int(input('Enter first side: '))
    b = int(input('Enter second side: '))
    c = int(input('Enter third side: '))
    areaV, perimeterV = area(a, b, c)
    # The output statements. 
    print ("The area is:", format(areaV,',.1f'),"and the perimeter is:", format(perimeterV, ',.1f'))

Upvotes: 2

Hugh Bothwell
Hugh Bothwell

Reputation: 56634

You cannot call areaV, perimeterV = area(a, b, c) until you have assigned values to a, b, c.

Upvotes: 0

Related Questions