Now You Code 3: Area and Circumference

In this assignment you are tasks with writing a program which inputs the radius of a circle, and outputs the circumference and area of that circle. Feel free to lookup the formulas online.

We will use the Write - Refactor - Test - Rewrite approach to complete the final solution which uses functions.

NOTE import math then use math.PI to get the value of Pi https://en.wikipedia.org/wiki/Pi

Step 1: Write

Here's the algorihm of this program. Write the solution in Python.

    input radius as a float
    calculate circle area
    calculate circle circumference
    print radius, area, and circumference

Example run:

Enter Radius: 1
A circle with radius 1.000000 has area 3.141593 and circumference 6.283185

In [14]:
import math
## TODO: Write the solution here Step 1

Step 2: Refactor

Next we need to refactor the area and circumfrence logic into functions.

  • CircleArea() has one input a radius and returns the area as output.
  • CircleCircumference() has one input a radius and returns the circumference as output.

In [13]:
## Step 2: Refactor into two functions def CircleArea... and def CircleCircumference

Step 3: Test

Time to test. Write two tests, once for each function

WHEN radius=1 We EXPECT CircleArea(radius) to return 3.141593 
WHEN radius=1 We EXPECT CircleCircumference(radius) to return 6.283185

Make sure you call the function to get the actual, just like in the coding lab.

NOTE: If its accurate to 4 decimal places, that's sufficient. The test values might be different because of how the "%f" format code works.


In [12]:
# Step 3: Write tests.

Step 4: rewrite the program to use the function

Finally re-write the original program, but call the functions.


In [11]:
## Step 4: Write program here from the algorithm above

Step 5: Questions

  1. Provide 3 advantages for programming a user function in your code like CircleArea or CircleCircumference

Answer:

  1. If we want to guarantee our CircleCircumference function is 100% accurate, what is the minimum number of test cases must we provide in step 3?

Answer:

Step 6: Reflection

Reflect upon your experience completing this assignment. This should be a personal narrative, in your own voice, and cite specifics relevant to the activity as to help the grader understand how you arrived at the code you submitted. Things to consider touching upon: Elaborate on the process itself. Did your original problem analysis work as designed? How many iterations did you go through before you arrived at the solution? Where did you struggle along the way and how did you overcome it? What did you learn from completing the assignment? What do you need to work on to get better? What was most valuable and least valuable about this exercise? Do you have any suggestions for improvements?

To make a good reflection, you should journal your thoughts, questions and comments while you complete the exercise.

Keep your response to between 100 and 250 words.

--== Write Your Reflection Below Here ==--