0

I am a beginner with Python and trying to calculate the area of a triangle. The task asks to define the area function, and I can't get it to return a valid value (should be float) to the main function.

from math import sqrt
def area(linea, lineb, linec):
    """function is supposed to find the area of the triangle"""
    s = (linea + lineb + linec) // 2
    a = sqrt(s * (s - linea) * (s - lineb) * (s - linec))
    return a

def main():
    linea = float(input("Enter the length of the first side: "))
    lineb = float(input("Enter the length of the second side: "))
    linec = float(input("Enter the length of the third side: "))
    b = float(a)
    print("The triangle's area is %0.2f" %b)

The print should give the area in 0.0 form. What should I do so that I don't get the error code "NameError:␣name␣'a'␣is␣not␣defined"?

emkay
  • 25
  • 3
  • 2
    you should do ```b = area(linea, lineb, linec)``` instead. Also you should use ```/``` not ```//``` in your definition of ```s``` in ```area``` and then call ```main()``` at the end – Hugh Mungus Jul 16 '22 at 18:06
  • You can find more details about `/` (division) and `//` (floor division that yields an integer) in the documentation https://docs.python.org/3/reference/expressions.html#binary-arithmetic-operations – Thierry Lathuille Jul 16 '22 at 18:15
  • variable a is LOCAL to the area function and not known to the main function hence the error. The same issue arises with linea etc which are not the same objects in main() and in area(). You need to check out scope rules. – user19077881 Jul 16 '22 at 19:51

1 Answers1

0

I tried out your code. Simply, I think you meant for the following line of code:

b = float(a)

to be:

b = float(area(linea, lineb, linec))

Regards.

NoDakker
  • 3,390
  • 1
  • 10
  • 11