Skip to content

Commit 4e2fcaf

Browse files
committed
Fixes #4710 added exceptions and tests
1 parent 07646ac commit 4e2fcaf

File tree

1 file changed

+60
-19
lines changed

1 file changed

+60
-19
lines changed

Diff for: physics/horizontal_projectile_motion.py

+60-19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@
2121
# Acceleration Constant on hearth (unit m/s^2)
2222
g = 9.80665
2323

24+
25+
def check_args(init_velocity: float, angle: float) -> None:
26+
"""
27+
Check that the arguments are valid
28+
"""
29+
30+
# Ensure valid instance
31+
if not isinstance(init_velocity, (int, float)):
32+
raise TypeError("Invalid velocity. Should be a positive number.")
33+
34+
if not isinstance(angle, (int, float)):
35+
raise TypeError("Invalid angle. Range is 1-90 degrees.")
36+
37+
# Ensure valid angle
38+
if angle > 90 or angle < 1:
39+
raise ValueError("Invalid angle. Range is 1-90 degrees.")
40+
41+
# Ensure valid velocity
42+
if init_velocity < 0:
43+
raise ValueError("Invalid velocity. Should be a positive number.")
44+
45+
2446
def horizontal_distance(init_velocity: float, angle: float) -> float:
2547
"""
2648
Returns the horizontal distance that the object cover
@@ -37,7 +59,16 @@ def horizontal_distance(init_velocity: float, angle: float) -> float:
3759
91.77
3860
>>> horizontal_distance(100, 78)
3961
414.76
62+
>>> horizontal_distance(-1, 20)
63+
Traceback (most recent call last):
64+
...
65+
ValueError: Invalid velocity. Should be a positive number.
66+
>>> horizontal_distance(30, -20)
67+
Traceback (most recent call last):
68+
...
69+
ValueError: Invalid angle. Range is 1-90 degrees.
4070
"""
71+
check_args(init_velocity, angle)
4172
radians = angle_to_radians(2 * angle)
4273
return round(init_velocity ** 2 * sin(radians) / g, 2)
4374

@@ -58,10 +89,18 @@ def max_height(init_velocity: float, angle: float) -> float:
5889
22.94
5990
>>> max_height(100, 78)
6091
487.82
92+
>>> max_height("a", 20)
93+
Traceback (most recent call last):
94+
...
95+
TypeError: Invalid velocity. Should be a positive number.
96+
>>> horizontal_distance(30, "b")
97+
Traceback (most recent call last):
98+
...
99+
TypeError: Invalid angle. Range is 1-90 degrees.
61100
"""
62-
101+
check_args(init_velocity, angle)
63102
radians = angle_to_radians(angle)
64-
return round((init_velocity ** 2 * sin(radians) ** 2) / (2 * g), 2)
103+
return round(init_velocity ** 2 * sin(radians) ** 2 / (2 * g), 2)
65104

66105

67106
def total_time(init_velocity: float, angle: float) -> float:
@@ -80,8 +119,16 @@ def total_time(init_velocity: float, angle: float) -> float:
80119
4.33
81120
>>> total_time(100, 78)
82121
19.95
122+
>>> total_time(-10, 40)
123+
Traceback (most recent call last):
124+
...
125+
ValueError: Invalid velocity. Should be a positive number.
126+
>>> total_time(30, "b")
127+
Traceback (most recent call last):
128+
...
129+
TypeError: Invalid angle. Range is 1-90 degrees.
83130
"""
84-
131+
check_args(init_velocity, angle)
85132
radians = angle_to_radians(angle)
86133
return round(2 * init_velocity * sin(radians) / g, 2)
87134

@@ -97,25 +144,19 @@ def test_motion() -> None:
97144

98145

99146
if __name__ == "__main__":
147+
from doctest import testmod
100148

101-
# Get input from user
102-
init_vel = float(input("Initial Velocity: "))
149+
testmod()
103150

104151
# Get input from user
105-
angle = float(input("angle: "))
152+
init_vel = float(input("Initial Velocity: ").strip())
106153

107-
# Ensure valid angle
108-
if angle > 90 or angle < 1:
109-
print("Error: Invalid angle. Range is 1-90 degrees.")
110-
111-
# Ensure valid velocity
112-
elif init_vel < 0:
113-
print("Error: Invalid velocity. Should be a positive number.")
154+
# Get input from user
155+
angle = float(input("angle: ").strip())
114156

115157
# Print results
116-
else:
117-
print()
118-
print("Results: ")
119-
print(f"Horizontal Distance: {str(horizontal_distance(init_vel, angle))} [m]")
120-
print(f"Maximum Height: {str(max_height(init_vel, angle))} [m]")
121-
print(f"Total Time: {str(total_time(init_vel, angle))} [s]")
158+
print()
159+
print("Results: ")
160+
print(f"Horizontal Distance: {str(horizontal_distance(init_vel, angle))} [m]")
161+
print(f"Maximum Height: {str(max_height(init_vel, angle))} [m]")
162+
print(f"Total Time: {str(total_time(init_vel, angle))} [s]")

0 commit comments

Comments
 (0)