In Newtonian physics, free fall “is any motion of a body where gravity is the only force acting upon it”. In other words, it is the motion of some object, that moves (or falls) by the force of gravity. For this assignment, you will use math, if-statements, and while-loops to model falling and bouncing balls using some very basic physics calculations. To be clear, this is not a physics class, and you are not expected to have a strong physics background. However, modeling this physical behavior can provide a good opportunity to see how the features we have covered in python thus far can be used to model the physical world. Those of you that have taken physics might have done an experiment to model a falling object, such as a tennis ball. The small amount of physics required for this will be explained, or simplified, where necessary.
This program is going to model the behavior of a ball being dropped from various floors of a multi-story building. Thus, the first input to the program should be the number of floors of the building. The second input should be a number, indicating how many points the program should calculate the drop time and bounce from. The last input should be the number of ball bounces that the program should calculate the height of, for each drop. Getting the inputs should look like so:
--- Welcome to the free fall calculator ---
Number of building stories:
4
Number of drop points:
2
Number of bounce heights calculate:
3
Each of the three integers shown (4, 2, and 3, in this case) represent the inputs given by the user. There are some restrictions on the input, which you are required to check for. The number of stories must be a multiple of the number of drop points. What follows from this is that the drop point value cannot be greater than the number of stories. Thus, for a 4 story building, 4, 2, and 1 are acceptable inputs, but 3, 5, 6, 7 and so on would not be acceptable. If this rule is broken, the program should print the message shown below, and then exit.
Number of stories required to be a multiple of the drop points.
Also, all three of the input numbers must be 50 or less. You can check for all three of these with a single if-statement. If any are above 50, then print out the message shown below, and also exit.
Input values required to be 50 or less.
If all of the input values are valid, the program should proceed to do several things:
For instance, the inputs 4, 2, and 3 should produce the below output:
.-------.
| # # # | <-(1)
| |
| # # # |
| |
| # # # | <-(2)
| |
| # # # |
| |
*********
(1) fall time = 1.81 seconds, bounce 1 = 11.2, bounce 2 = 7.84, bounce 3 = 5.49
(2) fall time = 1.28 seconds, bounce 1 = 5.6, bounce 2 = 3.92, bounce 3 = 2.74
The first part is the building (the hash-tags representing windows), along with labels for the drop points (<-(1)
and <-(2)
in this case).
Notice that a 4-story building is pictured, two drop points are shown, and information about the initial drop time and first three bounces is shown in the legend at the end.
Another example, with different inputs, is shown below.
--- Welcome to the free fall calculator ---
Number of building stories:
6
Number of drop points:
3
Number of bounce heights calculate:
2
.-------.
| # # # | <-(1)
| |
| # # # |
| |
| # # # | <-(2)
| |
| # # # |
| |
| # # # | <-(3)
| |
| # # # |
| |
*********
(1) fall time = 2.21 seconds, bounce 1 = 16.8, bounce 2 = 11.76
(2) fall time = 1.81 seconds, bounce 1 = 11.2, bounce 2 = 7.84
(3) fall time = 1.28 seconds, bounce 1 = 5.6, bounce 2 = 3.92
How to determine which floors should be the drop point floors, and how to calculate the height of a floor, will be described later.
Now, let’s talk about how to calculate the initial fall time, and the bounce heights. Like I mentioned, you are not expected to be physics experts, so I will provide you with the formula for calculating the time of a fall. The formula for calculating the time is:
The value t is what shall be computed, the time of the fall!
The value d is the distance, in meters, of the fall.
The value g is the acceleration due to gravity, which is the constant value 9.807
.
Thus, to calculate the fall time, you just need to code-ify this formula, and plug in the height in meters.
For this PA, you can assume that each floor is 4 meters.
Thus, dropping a ball from the 3rd floor would be a 12 meter drop.
Note that this simple formula does not perfectly model the real world.
For instance, it does not take into account wind resistance.
However, it is a close enough estimate for our purposes.
Now for the ball bounces. In reality, modelling the height of ball bounces is not a simple task. One needs to take into account factors such as material of the ball, material of the surface it bounces against, and so on. You can read more about it on this website. For this PA, we will make a simplification. We will assume that for each bounce after the initial drop, the ball will reach 70% of the drop height. Thus, if a ball is dropped from 12 meters, it will reach 8.39 meters at the peak of the first bounce (12 * 0.7), 5.87 meters on the second ( 12 * 0.7 * 0.7), and so on.
If you try to tackle this problem in one fell swoop, you might get overwhelmed. Thus, I provide this development strategy. I recommend that you follow the steps show here. This development strategy doesn’t show you everything you need to do, but it is a good guide for staying on-track. Also, this is not the type of PA where you have to finish the entire thing before passing any test cases. There will be test cases that you should be able to pass after completing steps 2, 3, 5, and 6. Thus, test your program out as you make your way through the steps. Say, for example, that you only make it to step 5 by the time it is due. You still might be able to pass a number of tests!
Read the whole spec before starting to code. Seriously, just read it :)
Create your program file (freefall.py
), and write the code to get the three inputs.
After you’ve gotten them, validate the input according to the rules given previously in the spec.
If you need to stop the program, remember to use the os._exit()
function from the os
module.
Using the first input (the number of floors), print out a building of the correct size. Make sure the formatting, number of floors, and top and bottom are correct, and that the size changes when different floor values are provided. You are not allowed to user string multiplication for this, use a while loop instead. There will be some test cases with 0 as the input for drop points, which you can use to test if your building is formatted OK.
Next, you should work on getting the floor markers showing up on the correct floors next to the building.
The first floor marker will always show up at the top floor.
The other markers should show up at consistent intervals, based on the total number of drop points.
You can determine the spacing by calculating (number_of_floors / drop_points)
.
For instance, if the number of floors is 12 and the user wants 3 drop points, then there should be a marker every 4 floors (one on floor 12, one on floor 8, and one on floor 4).
For this step, write another while loop, which you can use to calculate the time of each drop.
The loop can be structured similarly to the building-drawing loop, except on the iterations where you would have printed out a marker, instead, calculate the fall time using the formula, and print out the number.
Round each result, both for drop time and for bounces in the next step, to two decimal places with the round
function.
There will be some test cases that give 0 for the bounce calculation, thus you should be able to pass those after you finish step 5.
Now, for each drop point, you should calculate N bounces, where N is the value of the third input. In order to do this, you should write a loop within the loop you already wrote to calculate the drop times. For each of the drop time results, you can have a loop that repeats N times, and on each iteration, multiplies the height by 0.7, and displays the result.
You should include a comment at the top of the code file, follow the variable naming rules, and try to write clean code. For more details on how to do this, you can reference the style guide on the class website.
For this assignment:
break
statementmath
moduleThis PA is due on Tuesday, February 11th at 7pm. Turn in the program via Gradescope. You should do your best to get your program to pass all of the test cases.