heron.py

Created by vnap0v

Created on May 01, 2025

534 Bytes

Defines a function heronarea() which calculates the area of a triangle in terms of the three side lengths. The function can be called with the three lengths as parameters or without parameters. This code can run on CPython as well. Tested on the calculator using software version 23.2.6.


from math import *
def heronarea(a=0,b=0,c=0):
  t1="s=0.5*(a+b+c)"
  t2="A=sqrt(s*(s-a)*(s-b)*(s-c))"
  print("Herons formula for area of a triangle\n"+t1+"\n"+t2)
  if a==0:
    sides=[];txt=("a","b","c")
    for i in range(3):
      inp=input("length of side "+txt[i]+"? ")
      sides.append(float(inp))  
  else:
    sides=[a,b,c]
  s=sum(sides)/2
  A=1
  for side in sides:
    A*=(s-side)
  if s*A<0:
    print("not a valid triangle")
    return 
  A=sqrt(s*A)
  print("Area A = "+str(A))
  return A
  
heronarea()

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.