Mar-06-2018, 01:34 AM
(This post was last modified: Mar-06-2018, 01:34 AM by GamePatrol.)
I wrote a module that calculates the area of shapes and I wanted to get feedback if I'm on the right track before adding more shapes.
import math # A python program to calculate the area of shapes # area of a square is length multiplied by width def area_of_square(length, width): _check_if_values_are_strings(length = length, width = width) _check_if_values_are_negitive(lenth=length, width = width) return length * width # area of a triangle is base multiplied by height divided by 2 def area_of_triangle(base_value, height): _check_if_values_are_strings(base_value = base_value, height = height) _check_if_values_are_negitive(base_value = base_value, height = height) return (base_value * height) / 2 # area of a circle is radius squared times pi def area_of_circle(radius): _check_if_values_are_strings(radius = radius) _check_if_values_are_negitive(radius = radius) return math.pow(radius,2) * math.pi def _check_if_values_are_strings(**values): for k,v in values.items(): if (isinstance(v,str)): raise TypeError("{0} is a string".format(k)) def _check_if_values_are_negitive(**values): for k,v in values.items(): if v < 0: raise ValueError("{0} is a negitive".format(k))