Question: problem a Write a program (script) that calculates how far an object has fallen after a given time.Assume that it is stationary and that you
problem a
Write a program (script) that calculates how far an object has fallen after a given time.Assume that it is stationary and that you release it at time 0. The program should
a.Read a number from the user, which is the number of seconds the object has fallen.
b.Calculate the speed in meters per.second after the number of seconds with the formula speed = acceleration * time.The acceleration from gravity is 9.81 m / s2.
c.Calculate the distance the object has fallen in meters with the formula distance = 0.5 * speed * time
d.Print speed and distance.
answer:
GRAVITY = 9.81
from_user = input ("Enter the number of seconds the object has dropped:")
number_seconds = float (from_user)
speed = GRAVITY * number_seconds
distance = 0.5 * speed * number_seconds
print (f "After {number_seconds} seconds, the object has dropped {distance: .2f} and falls with speed {speed: .2f}")
problem b
Rewrite the program from problem a) so that it checks that the number the user has entered is a number
greater than 0, and prints an error message if it is not greater than 0.
answer:
GRAVITY = 9.81
from_user = input ("Enter the number of seconds the object has dropped:")
number_seconds = float (from_user)
if number_seconds> = 0.0:
speed = GRAVITY * number_seconds
distance = 0.5 * speed * number_seconds
print (f "After {number_seconds} seconds, the object has dropped {distance: .2f} and falls with speed {speed: .2f}")
else:
print ("Number of seconds cannot be negative!")
problem c
Extend the program from task b) to print speed and distance for more times.The program should ask the user for a time interval (for example, 2 seconds) and a number of intervals (for example, 10).Then the program should calculate speed and distance for each interval and print it out.In the example, the program should calculate and print speed and distance after 2, 4, 6, 8, 10, 12, 14, 16, 18 and 20 seconds.
answer:
GRAVITY = 9.81
userinput = input ("Enter the interval size:")
interval size = int (user input)
userinput = input ("Enter number of intervals:")
number_intervals = int (user input)
if number_intervals> = 0.0 and interval size> 0.0:
for number_seconds in range (interval size,interval size * (number_intervals) + 1,interval size):
speed = GRAVITY * number_seconds
distance = 0.5 * speed * number_seconds
print (f "After {number_seconds} seconds the object has dropped {distance: .2f}"
f "and falls with speed {speed: .2f}")
else:
print ("Number of intervals can not be negative! Interval size must be""positive!")
problem d
Lists: rewrite the program in problem c) so that it plots the result in a graph with matplotlib instead of printing it to the console.You just need to plot the distance dropped as a function of time.This requires you to create two lists, a list of times and a list of distances.In the for-loop, enter values in the two lists.After the for-loop is complete, use the two lists when plotting.The times become the x-axis and the distances become the y-axis.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
