21
21
# Acceleration Constant on hearth (unit m/s^2)
22
22
g = 9.80665
23
23
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
+
24
46
def horizontal_distance (init_velocity : float , angle : float ) -> float :
25
47
"""
26
48
Returns the horizontal distance that the object cover
@@ -37,7 +59,16 @@ def horizontal_distance(init_velocity: float, angle: float) -> float:
37
59
91.77
38
60
>>> horizontal_distance(100, 78)
39
61
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.
40
70
"""
71
+ check_args (init_velocity , angle )
41
72
radians = angle_to_radians (2 * angle )
42
73
return round (init_velocity ** 2 * sin (radians ) / g , 2 )
43
74
@@ -58,10 +89,18 @@ def max_height(init_velocity: float, angle: float) -> float:
58
89
22.94
59
90
>>> max_height(100, 78)
60
91
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.
61
100
"""
62
-
101
+ check_args ( init_velocity , angle )
63
102
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 )
65
104
66
105
67
106
def total_time (init_velocity : float , angle : float ) -> float :
@@ -80,8 +119,16 @@ def total_time(init_velocity: float, angle: float) -> float:
80
119
4.33
81
120
>>> total_time(100, 78)
82
121
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.
83
130
"""
84
-
131
+ check_args ( init_velocity , angle )
85
132
radians = angle_to_radians (angle )
86
133
return round (2 * init_velocity * sin (radians ) / g , 2 )
87
134
@@ -97,25 +144,19 @@ def test_motion() -> None:
97
144
98
145
99
146
if __name__ == "__main__" :
147
+ from doctest import testmod
100
148
101
- # Get input from user
102
- init_vel = float (input ("Initial Velocity: " ))
149
+ testmod ()
103
150
104
151
# Get input from user
105
- angle = float (input ("angle : " ))
152
+ init_vel = float (input ("Initial Velocity : " ). strip ( ))
106
153
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 ())
114
156
115
157
# 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