Home > Blockchain >  Python program that displays height of a ball that is thrown. The program doesn't output anythi
Python program that displays height of a ball that is thrown. The program doesn't output anythi

Time:11-01

The height of an object, in metres, t seconds after it is launched straight up into the air is given by the equation h = -4.9t^2 bt c where b represents the ball’s initial speed in metres per second and c represents the ball’s initial height in metres. Using the elif statement, write a Python program that displays the height of the ball every second from the instant it is thrown until it hits the ground.

Design your program such that the following criteria are satisfied: • The user enters the initial speed and the initial height. • The format of the output is as follows: After 2 seconds, the height is 30.87 m. • Instead of displaying negative height values, the output states that the ball is on the ground. • The output does not contain grammatical errors, such as “After 1 seconds,....”

My code is the following:

#initial values

t = 0

h = 0

#ask user for initial speed and initial height

b = float(input("What is the initial speed in metres per second?: "))

c = float(input("What is the initial height in metres: "))


#calculate and display height 

while h>=0:
 
  h = round(-4.9*t**2 b*t c, 2)

if t==1:

  print("After 1 second, the height is", h, "m.")

elif h>0:

  print("After", t, "seconds, the height is:", h, "m.")
  
else:

  print("After", t,"seconds, the ball is on the ground")

t = t 1

The code runs but after the user inputs the initial speed and height the program keeps running and doesn't output anything.

CodePudding user response:

Bad formatting. All those if and print statements, and the increment of t, have to be indented to be inside the while loop.

t = 0
h = 0
b = float(input("What is the initial speed in metres per second?: "))
c = float(input("What is the initial height in metres: "))
while h>=0: 
    h = round(-4.9*t**2 b*t c, 2)
    if t==1:
        print("After 1 second, the height is", h, "m.")
    elif h>0:
        print("After", t, "seconds, the height is:", h, "m.")
    else:
        print("After", t,"seconds, the ball is on the ground")
    t = t 1

You should use an IDE like PyCharm instead of a mere text editor.

Here's the output I get. Your physics are simple. They seem correct.

What is the initial speed in metres per second?: >? 0
What is the initial height in metres: >? 50
After 0 seconds, the height is: 50.0 m.
After 1 second, the height is 45.1 m.
After 2 seconds, the height is: 30.4 m.
After 3 seconds, the height is: 5.9 m.
After 4 seconds, the ball is on the ground

Should be easy to calculate the precise time of impact. Set h = 0 and solve for time. It's a quadratic equation, so there should be two roots. Take the one that makes physical sense - time is a real, positive number.

CodePudding user response:

while h>=0:
    h = round(-4.9*t**2 b*t c, 2)

The way your code is indented, this is a loop all by itself, and it never ends because h is always >= 0.

  • Related