Answer to Question #242525 in Python for prosper2000

Question #242525
Part 2

Invent your own function that does some useful computation of your choosing. Do not copy the function from somewhere else. Use incremental development, and record each stage of the development process as you go. Finally, print output that demonstrates that the function works as you intended.

Include all of the following in your Learning Journal:

An explanation of each stage of development, including code and any test input and output.
The output of three calls to your function with different arguments.
1
Expert's answer
2021-09-26T18:50:02-0400

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



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS