kinetic_energy.py
· 1.0 KiB · Python
Raw
#!/usr/bin/env python3
import argparse
# get kinetic energy for any object you know the speed and weight
# To use, just run python3 kinetic_energy.py --speed 30 --weight 1500
# speed is in Km/h, weight in Kg
def calculate_energy(weight, velocity):
# Convert weight from kg to Newtons (1 kg = 9.81 N)
mass = weight * 9.81
# Calculate kinetic energy (in Joules)
kinetic_energy = 0.5 * mass * (velocity**2)
return kinetic_energy
def convert_kmh_to_ms(kmh):
return kmh * (1000 / 3600)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Calculate kinetic energy of a moving vehicle"
)
parser.add_argument(
"-s", type=float, required=True, help="Velocity in kilometers per hour"
)
parser.add_argument("-w", type=float, required=True, help="Weight in kilograms")
args = parser.parse_args()
velocity_km = args.s
velocity_ms = convert_kmh_to_ms(velocity_km)
weight = args.w
energy = calculate_energy(weight, velocity_ms)
print(f"Kinetic Energy: {energy:.2f} J")
1 | #!/usr/bin/env python3 |
2 | |
3 | import argparse |
4 | |
5 | # get kinetic energy for any object you know the speed and weight |
6 | # To use, just run python3 kinetic_energy.py --speed 30 --weight 1500 |
7 | # speed is in Km/h, weight in Kg |
8 | |
9 | |
10 | def calculate_energy(weight, velocity): |
11 | # Convert weight from kg to Newtons (1 kg = 9.81 N) |
12 | mass = weight * 9.81 |
13 | |
14 | # Calculate kinetic energy (in Joules) |
15 | kinetic_energy = 0.5 * mass * (velocity**2) |
16 | |
17 | return kinetic_energy |
18 | |
19 | |
20 | def convert_kmh_to_ms(kmh): |
21 | return kmh * (1000 / 3600) |
22 | |
23 | |
24 | if __name__ == "__main__": |
25 | parser = argparse.ArgumentParser( |
26 | description="Calculate kinetic energy of a moving vehicle" |
27 | ) |
28 | parser.add_argument( |
29 | "-s", type=float, required=True, help="Velocity in kilometers per hour" |
30 | ) |
31 | parser.add_argument("-w", type=float, required=True, help="Weight in kilograms") |
32 | args = parser.parse_args() |
33 | |
34 | velocity_km = args.s |
35 | velocity_ms = convert_kmh_to_ms(velocity_km) |
36 | weight = args.w |
37 | |
38 | energy = calculate_energy(weight, velocity_ms) |
39 | |
40 | print(f"Kinetic Energy: {energy:.2f} J") |