Short Project 3

Calculating areas

In this short project you will write four functions to calculate the areas of different geometric shapes. Name the program geometry.py. Make sure that gradescope gives you the points for passing the test cases.

Area of a Rectangle – rectangle_area

This function returns the area of the rectangle with given base and height

\[ area = base \cdot height \]

Area of a Triangle – triangle_area

This function returns the area of three given triangle side lengths calculated according to Heron’s formula (calculate the semiperimeter first, then use that to calculate the area):

\[ s = (a + b + c) / 2 \]

\[ area = \sqrt{s \cdot (s-a) \cdot (s-b) \cdot (s-c)} \]

Do not use any built-in method or function for any Python library. Remember that roots are the opposite of an exponent. So you can calculate the square root of a number by using the exponent form \(n^{1/2}\).

Area of a Trapezoid – trapezoid_area

This function returns the area of the trapezoid with given base_1, base_2, and height.

\[ area = {1/2} \cdot (base_1 + base_2) \cdot height \]

Area of a Circle – circle_area

This function returns the area of the circle with the given radius, rounded at two decimal places. Use the value 3.1415 for \(\pi\).

\[ area = \pi \cdot radius^2 \]

Any area – calculate_area

This function calls the other functions, based on a string argument: "rectangle", "triangle", or "circle". It returns a string that looks like this:

The area of the triangle is 6.0

This function takes a total of four arguments, the first one is a string that tells the function which area to calculate, and the three other parameters are numeric. In the case of the circle, for example, only the first numeric argument is used.

Test cases

def main():
    print( rectangle_area(4, 4.5) ) # 18.0
    print( triangle_area(3, 4, 5) ) # 6.0
    print( trapezoid_area(4, 20, 10) ) # 120.0
    assert circle_area(20) == 1256.6

    message = calculate_area("trapezoid", 11, 25, 5)
    print(message) # "The area of the trapezoid is 90.0"
    
    message = calculate_area("circle", 4, 0, 0)
    print(message) # "The area of the circle is 50.26"
  
main()
Before You Begin

This section gives you a quick recap of what we covered in class or introduces any new tips or examples that might help you complete the assignment. Take a few minutes to read through it before you begin.

Writing a function

  • Start with def followed by the name of the function
  • Add parameters inside the parentheses in the function definition
  • Return a value
def double(n):
  result = n * 2
  return result

def main():
  print( double(5) )

main()
10

Using round()

Use the round() function to get a floating-point number rounded to the specified number of decimals.

Syntax:

round(number, ndigits*)

The number of digits (ndigits) is optional, but we will often round number to two decimals:

round(392.68750000000006)
393
round(392.68750000000006, 2)
392.69

Using format()

We can do concatenation in two different ways: a) one using the + operator and b) the other using .format() method, using {} as place holders for our variables:

age = 20
name = 'Philip'

print(name + ' is ' + str(age) + ' years old')
print('{} is {} years old'.format(name, age))
Philip is 20 years old
Philip is 20 years old

If statements

We can use logical expressions in if statements to return different things from a function:

def is_positive(number):
  if number > 0:
    return "Number is positive"
  if number < 0:
    return "Number is negative"
  return "Number is zero"

def main():
  print( is_positive(10) )
  print( is_positive(0) )
  print( is_positive(-10) )
  
main()
Number is positive
Number is zero
Number is negative