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
- Reopen lab02bcd.py:
- [local] Go back to the IDLE window that contains your source code.
- [remote] Open your file in emacs again by typing:
emacs lab02bcd.py
- Since you are extending your program, do not delete any of your existing code as you follow
the instructions below!
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- Execute the program several times to complete the table in Question 7 of your Moodle writeup.
- Each time you execute the program, you will be prompted to enter a value.
When that happens, type in the information in a row of the Input Value column,
exactly as it appears, then press ENTER.
- Record the value that your program reports for the area of the circle. The area of the
square should be the same as before.
- Continue to Part D.