Files
004_comission/saralai19981321/task1/Q4/main.py
louiscklaw 04a4edce00 update,
2025-01-31 21:40:42 +08:00

41 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
import os,sys
from pprint import pprint
from math import sin, pi
import random
# ## Instructions (for reference):
# 1. Initialize the height of the particle to 100 meters.
h = 100
# 2. Use a while loop to simulate the motion of the particle until it hits the ground.
# 3. Inside the while loop, use an if-else statement to check if the particle has hit the ground.
# 4. Update the height of the particle using the equation of motion: h = hinitial 1/2 * g * (t)2.
# 5. Print the height of the particle at each time step.
# 6. Use a for loop to iterate through the time steps.
# h = h - 1/2 * g * t^2
# 1. Initialize the height from which the particle is released.
t_step = float(input("Enter the time step in seconds: "))
h_current = float(input("Enter the height in meters: "))
h_initial = h_current
t_current = 0
while (h_current >= 0):
# 4. Print the height of the particle at each time step until it hits the ground.
print(f'Time: {t_current:.1f} s, Height: {h_current:.2f} m')
# 2. Use a loop to update the position of the particle at each time step.
t_current += t_step
h_current = h_initial - (1/2 * 9.8 * (t_current**2))
# 3. Use an if-else statement to check if the particle has hit the ground.
if (h_current < 0):
print(f'Time: {(t_current):.1f} s, Height: {0:.2f} m')
print(f'has hit the ground')
break