01 Class Activity: Review Python
Purpose
Write a Python program that gets input from a user, uses
variables, performs arithmetic using the math
module,
and displays results for the user to see.
Problem Statement
The time in seconds that a pendulum takes to swing back and forth once is given by this formula:
- t is the time in seconds,
- π is the constant PI, which is the ratio of the circumference of a circle divided by its diameter (use
math.pi
), - h is the length of the pendulum in meters.
Activity
Write a program named pendulum.py
that prompts a
user to enter the length of a pendulum in meters and then computes
and prints the time in seconds that it takes for that pendulum to
swing back and forth. To start your program, copy and paste the
following code into your program and use it as an outline as you
write code. Note that in a Python program, a triple quoted string at
the top of the file acts as a comment for the entire program.
""" The time in seconds that a pendulum takes to swing back and forth once is given by this formula: ____ / h t = 2π / ---- √ 9.81 t is the time in seconds, π is the constant PI, which is the ratio of the circumference of a circle divided by its diameter (use math.pi), h is the length of the pendulum in meters. Write a program that prompts a user to enter the length of a pendulum in meters and then computes and prints the time in seconds that it takes for that pendulum to swing back and forth. """
Helpful Documentation
- The preparation content for this lesson explains how to write code to do the following:
- Get input from a user
- Convert user input from a string to a number
- Display results for the user to see
- The Python
math
module contains mathematical constants and functions includingmath.pi
andmath.sqrt()
.
Testing Procedure
Verify that your program works correctly by following each step in this testing procedure:
- Run your program and enter the input shown below. Ensure that your program’s output matches the output below.
> python pendulum.py Length of pendulum (meters): 1.5 Time (seconds): 2.46
Sample Solution
Please work diligently with your class to complete this activity. After class is over, please compare your solution to the sample solution. Please do not look at the sample solution until you have either finished the program or diligently worked with your class.
Ponder
During this assignment, you wrote a Python program that gets input from a user, uses variables, performs arithmetic, and displays results for the user to see. Because you should have learned how to write this type of program in CSE 110, this assignment should have been fairly easy for you. If this assignment was difficult for you, you should review the concepts from CSE 110 and the programs that you wrote in that course.