In: Computer Science
A standard science experiment is to drop a ball and see how high it bounces. Once the “bounciness” of the ball has been determined, the ratio gives a bounciness index.
For example, if a ball dropped from a height of 10 feet bounces 6 feet high, the index is 0.6, and the total distance traveled by the ball is 16 feet after one bounce. If the ball were to continue bouncing, the distance after two bounces would be 10 ft + 6 ft +6 ft + 3.6 ft = 25.6 ft. Note that the distance traveled for each successive bounce is the distance to the floor plus 0.6 of that distance as the ball comes back up.
Write a program that lets the user enter the initial height from which the ball is dropped, the bounciness index, and the number of times the ball is allowed to continue bouncing. Output should be the total distance traveled by the ball.
Below is an example of the program input and output:
Enter the height from which the ball is dropped: 25 Enter the bounciness index of the ball: .5 Enter the number of times the ball is allowed to continue bouncing: 3 Total distance traveled is: 65.625 units.
Please explain in Python language. Thanks in advance!
Let us first make you understand the mathematics behind the problem:
Suppose the initial height is 25, the bouncinessIndex = 0.5 and the ball can be dropped 3 times
So initial the ball travelled 25metre while dropping and it jumped to 12.5 metres
now the ball will drop from 12.5-metre height and will bounce to 6.25 metres
now the ball will drop from 6.25 metre and jump to 3.125 metre
So the total distance travelled will be 25.0+ 12.5+ 12.5 +6.25+ 6.25+ 3.125 = 65.625 meter
Here is the code to do so:
if you if the code helped you please upvote it, for any doubts post a comment, I will surely help.
Code:
height = float(input("Enter the height from which the ball is
dropped: "))
bouncinessIndex = float(input("Enter the bounciness index of the
ball: "))
allowedBounces = int(input("Enter the number of times the ball is
allowed to continue bouncing: "))
# For every bounce add the height it was dropped from
# and the height it jumpes to
# Then change the value of height to the height it jumps to
result = 0
for i in range(allowedBounces):
print(height)
print(bouncinessIndex*height)
result += (height + bouncinessIndex*height)
height = bouncinessIndex*height
print(result)