2
### 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?

1
  • 2
    You need to put the areaV, perimeterV = ... line after the part where you create a, b, and c. Commented Feb 16, 2015 at 21:38

2 Answers 2

2

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'))
Sign up to request clarification or add additional context in comments.

2 Comments

Because areaV and perimeterV are returned values, they can't be called in the formatting as functions of the three side lengths.
Thanks, this worked perfectly. Will keep this in mind next time!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.