Feb-23-2022, 10:51 PM
Hello everyone. This is my first post so forgive me if I do something wrong.
Here's the problem given to me by my professor:
The coefficient of restitution of a ball, a number between 0 and 1, specifies how much energy is
conserved when the ball hits a rigid surface. A coefficient of 0.9 for instance, means a bouncing ball will
rise to 90% of its previous height after each bounce.
Write a program to input a coefficient of restitution and an initial height in meters, and report how many
times a ball bounces when dropped from its initial height before it rises to a height of less than 10
centimeters. Also report the total distance traveled by the ball before this point.
This is my code:
Thanks
Here's the problem given to me by my professor:
The coefficient of restitution of a ball, a number between 0 and 1, specifies how much energy is
conserved when the ball hits a rigid surface. A coefficient of 0.9 for instance, means a bouncing ball will
rise to 90% of its previous height after each bounce.
Write a program to input a coefficient of restitution and an initial height in meters, and report how many
times a ball bounces when dropped from its initial height before it rises to a height of less than 10
centimeters. Also report the total distance traveled by the ball before this point.
This is my code:
dis = 0 num = 0 try: coif_res = float(input("Enter the Coefficient of restitution ")) h = float(input("Enter Height in meters: ")) if coif_res < 0 or coif_res > 1: print("Coefficient of restitution must be between 0 and 1!") else: while h > .1: dis = dis + h h = coif_res * h num = num + 1 except (NameError, ValueError): print("Invalid Entry") if h < .1: print("Total distance in meters: ", "{:,.5f}".format(dis), "\nNumber of times bounced: ", "{:,.0f}".format(num))I have two problems. First, my math is wrong. The number of bounces is correct, but for some reason it isn't adding the height correctly. Second, my exception handler isn't working and I have no idea why. I've been working on this for a couple of hours now, any help is greatly appreciated.
Thanks