Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions