Requirements:
The task is to build a simple function to calculate the area of the circle inscribed
in a square with the certain side length
Design:
The solution to this task is simple: the area of the circle inscribed
in a square with a certain side length, a is equal to pi*(a/2)^2. However pi is not a native python variable. We can find pi in the math module that we need to import in advance.
The desired function will return the area value to the user
Implementation 1:
from math import pi
def circ_area_in_sq(a):
a = float(a)
return (pi*((a/2)**2))
Testing 1:
>>print(circ_area_in_sq(0))
>>print(circ_area_in_sq(1))
>>print(circ_area_in_sq(2))
0.0
0.7853981633974483
3.141592653589793
Communication 2:
It turned out that in some cases the function returns an exception. When the input is not a number the customer requires the function to return None
Design 2:
To handle this case an error handling has to be implemented.
Implementation 2:
from math import pi
def circ_area_in_sq(a):
try:
a = float(a)
return (pi*((a/2)**2))
except ValueError:
return None
Testing 2:
>>print(circ_area_in_sq("not a number"))
>>print(circ_area_in_sq(3))
>>print(circ_area_in_sq(4))
None
7.0685834705770345
12.566370614359172
Delivery 2:
The code was delivered and deployed
Comments
Leave a comment