1. A new tool!
Predict the output of the following program and then run it to see what happens. What operation does math.sqrt() perform?
2. Over the top?
Modify and run the program above to help answer the following questions:
- If you replace
x = math.sqrt(25)with simplyx = sqrt(25), does the program still run correctly? - Is the first line of the program necessary?
3. The root of the problem.
Rachel created the following program to calculate and display the side length of a square based on its area. Although her math is correct, her program contains two errors. Edit the code to fix the errors and get Rachel’s program running properly!
4. Secure the perimeter.
Modify the program above such that it outputs the square’s perimeter instead of its side length. Be sure to test your work!
5. A whole new toolbox!
Python’s math module (import math) gives us access to a whole bunch of mathematical functions and popular constants! For example, we can use math.pi for a highly accurate approximation of π (15 decimal places, which is adequate for NASA’s highest accuracy calculations). Run the following program to see!
6. Another slice of pi.
The following program calculates and displays the value of a circle’s area based on the radius value entered by the user (A = πr²). Modify the program such that it fetches the value of π from the math module instead of using the 3.14 approximation. Run your program to make sure it works!
7. Speaking volumes.
Modify the program above such that it calculates the volume of a cylinder based on the radius and height values entered by the user (in metres). Display the computed volume rounded to one decimal place and with the correct unit of measurement.
HINT: V = πr²h
8. Your turn.
Use the Python editor below to write and test a program that calculates and displays the length of the hypotenuse of any right triangle after the user inputs the lengths of its shorter sides (in inches). Be sure to
- use variables to represent each of the triangle’s side lengths.
- allow the user to enter decimal values.
- display the computed hypotenuse length rounded to two decimal places and with the correct unit of measurement.
HINT: c² = a² + b²