Clock Angle Calculation

Take time (hours and minutes) and print the smaller angle between the hour and minute hands.

AdvancedPhase 1 - Level 5: Creative / Tricky Logical ScenariosExample 4 of 10
clock-angle-calculation.py
1# Take time
2hours = int(input("Enter hours (1-12): "))
3minutes = int(input("Enter minutes (0-59): "))
4
5# Calculate angles
6hour_angle = (hours % 12) * 30 + minutes * 0.5
7minute_angle = minutes * 6
8
9# Find smaller angle
10angle = abs(hour_angle - minute_angle)
11smaller_angle = min(angle, 360 - angle)
12
13print(f"Smaller angle: {smaller_angle} degrees")

Output

Enter hours (1-12): 3
Enter minutes (0-59): 30
Smaller angle: 75.0 degrees

Enter hours (1-12): 12
Enter minutes (0-59): 0
Smaller angle: 0.0 degrees

What's going on

Calculate angles of hour and minute hands.

Key Concepts:

Hour hand: (hours % 12) * 30 + minutes * 0.5
Minute hand: minutes * 6
Find absolute difference
Take smaller of angle and 360 - angle