CS 115 Lab 2, Part C: Compute the area of a circle

[Back to lab instructions]


Background

This section is a high-level overview. The "Instructions" section below will walk you through the process step by step.

In this part of the lab, you will extend your program from Part B to compute the area of a circle, given the length of the radius.

Recall that the area of a circle with radius r can be calculated as $A = \pi r^{2}$

Your new program will ask the user for only one numeric value. Then, it will use that value to compute the areas of both the circle and the square.

Here is a sample of what your revised program will do. The user's input is italicized and underlined.

Enter a numeric value: 10
The area of a square with side length 10.0 is 100.0.
The area of a circle with radius length 10.0 is 314.159.

To earn a perfect grade on the lab, your output will have to match the sample output exactly.


Instructions

  1. Reopen lab02bcd.py:
  2. Since you are extending your program, do not delete any of your existing code as you follow the instructions below!
  3. To do your computation, you can use the built-in definition of $\pi$ from the math library. Start by placing an import statement between the docstring and the def main():
    import math
    From now on, you will be able to use the constant math.pi whenever you need the value of $\pi$ for a calculation.
  4. In your current program, notice that the statement
    square_area = length * length
    computes the area of a square whose side length is length and saves the result in a variable called square_area.
  5. Just after that statement, add a similar statement to your program to calculate the area of a circle whose radius length is also length. Your statement should store the result of this calculation in a new variable with an appropriate name.
  6. After your current print statement, add a statement to the program to print the area of the circle. This statement should be similar to the print statement that prints the area of the square, but be sure to change the wording to match the sample output above.
  7. Notice the
    round(square_area, 3)
    in the original print statement. This will print the value of the square_area variable, rounded to 3 decimal places. Be sure that your circle_area variable is also rounded to 3 decimal places.
  8. Run your program, typing in the number 10 when prompted. Make sure your output looks like the example:
    Enter a numeric value: 10
    The area of a square with side length 10.0 is 100.0.
    The area of a circle with radius length 10.0 is 314.159.
    
  9. Execute the program several times to complete the table in Question 7 of your Moodle writeup.
  10. Continue to Part D.