Question: def calculate _ total _ distance ( initial _ height, bounciness _ index, num _ bounces ) : Calculate the total distance

def calculate_total_distance(initial_height, bounciness_index, num_bounces):
"""
Calculate the total distance traveled by a bouncing ball.
Parameters:
- initial_height: Initial height from which the ball is dropped.
- bounciness_index: Ratio of bounce height to drop height.
- num_bounces: Number of times the ball is allowed to bounce.
Returns:
- Total distance traveled by the ball.
"""
total_distance = initial_height # Initial drop
# FOR loop implementation
for _ in range(num_bounces):
bounce_height = initial_height * bounciness_index
total_distance +=2* bounce_height # Distance to the floor + Distance back up
initial_height = bounce_height
return total_distance
def calculate_total_distance_while(initial_height, bounciness_index, num_bounces):
"""
Calculate the total distance traveled by a bouncing ball using a WHILE loop.
Parameters:
- initial_height: Initial height from which the ball is dropped.
- bounciness_index: Ratio of bounce height to drop height.
- num_bounces: Number of times the ball is allowed to bounce.
Returns:
- Total distance traveled by the ball.
"""
total_distance = initial_height # Initial drop
bounce_count =0
# WHILE loop implementation
while bounce_count < num_bounces:
bounce_height = initial_height * bounciness_index
total_distance +=2* bounce_height # Distance to the floor + Distance back up
initial_height = bounce_height
bounce_count +=1
return total_distance
# User inputs
initial_height = float(input("Enter the initial height from which the ball is dropped (in feet): "))
bounciness_index = float(input("Enter the bounciness index (e.g.,0.6 for 60% bounce): "))
num_bounces = int(input("Enter the number of bounces: "))
# Calculate and display total distance using FOR loop
total_distance_for = calculate_total_distance(initial_height, bounciness_index, num_bounces)
print(f"Total distance traveled using FOR loop: {total_distance_for:.2f} feet")
# Calculate and display total distance using WHILE loop
total_distance_while = calculate_total_distance_while(initial_height, bounciness_index, num_bounces)
print(f"Total distance traveled using WHILE loop: {total_distance_while:.2f} feet")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!