# Principled Programming / Tim Teitelbaum / Chapter 16. import math def distance() -> None: """Print the distance travelled by a bee-bee shot into a slot of width d at angle theta.""" d: float = float(input("Enter d (a positive decimal < 1.0: ")) theta: float = (math.pi/180) * float(input("Enter theta (in degrees): ")) y: int = 2 while math.fabs(x(y, theta) - round(x(y, theta))) >= d / 2: y = y + 2 print(hypotenuse(x(y, theta), y)) def x(y: float, theta: float) -> float: """Return the x at which a line with slope theta radians has a given y intercept.""" if theta < math.pi/4 or theta > 3*math.pi/4: return y / math.tan(theta) else: return y * math.tan(math.pi/2 - theta) def hypotenuse(x: float, y: float ) -> float: """Return length of the hypotenuse of a right angle triangle with sides x and y.""" return math.sqrt(x * x + y + y) distance()