Question: def calculate _ total _ distance ( initial _ height, bounciness _ index, num _ bounces ) : Calculate the total distance
def calculatetotaldistanceinitialheight, bouncinessindex, numbounces:
Calculate the total distance traveled by a bouncing ball.
Parameters:
initialheight: Initial height from which the ball is dropped.
bouncinessindex: Ratio of bounce height to drop height.
numbounces: Number of times the ball is allowed to bounce.
Returns:
Total distance traveled by the ball.
totaldistance initialheight # Initial drop
# FOR loop implementation
for in rangenumbounces:
bounceheight initialheight bouncinessindex
totaldistance bounceheight # Distance to the floor Distance back up
initialheight bounceheight
return totaldistance
def calculatetotaldistancewhileinitialheight, bouncinessindex, numbounces:
Calculate the total distance traveled by a bouncing ball using a WHILE loop.
Parameters:
initialheight: Initial height from which the ball is dropped.
bouncinessindex: Ratio of bounce height to drop height.
numbounces: Number of times the ball is allowed to bounce.
Returns:
Total distance traveled by the ball.
totaldistance initialheight # Initial drop
bouncecount
# WHILE loop implementation
while bouncecount numbounces:
bounceheight initialheight bouncinessindex
totaldistance bounceheight # Distance to the floor Distance back up
initialheight bounceheight
bouncecount
return totaldistance
# User inputs
initialheight floatinputEnter the initial height from which the ball is dropped in feet:
bouncinessindex floatinputEnter the bounciness index eg for bounce:
numbounces intinputEnter the number of bounces:
# Calculate and display total distance using FOR loop
totaldistancefor calculatetotaldistanceinitialheight, bouncinessindex, numbounces
printfTotal distance traveled using FOR loop: totaldistancefor:f feet"
# Calculate and display total distance using WHILE loop
totaldistancewhile calculatetotaldistancewhileinitialheight, bouncinessindex, numbounces
printfTotal distance traveled using WHILE loop: totaldistancewhile:f feet"
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
