Skip to content

Instantly share code, notes, and snippets.

@JanneSalokoski
Last active August 29, 2015 14:16
Show Gist options
  • Save JanneSalokoski/ec17e738c0bca5ec4d30 to your computer and use it in GitHub Desktop.
Save JanneSalokoski/ec17e738c0bca5ec4d30 to your computer and use it in GitHub Desktop.
A program that asks for time, and calculates the angle of the clock hands for the given time.
#!/usr/bin/env python3
import sys
def get_clock_angle(hour, minute):
"""Counts the angle of given clock hands.
Takes two integers, hour and minute.
Returns a float, the angle."""
# Check if the given values are valid.
if hour < 1 or hour > 24:
print(str(hour) + " is not a valid value for hour")
sys.exit()
if hour > 12:
print("Converting " + str(hour) + " into 12-hour format: " + str(hour - 12))
hour = hour - 12
if minute > 59 or minute < 0:
print(str(minute) + " is not a valid value for minute")
sys.exit()
clock_degrees = 360
clock_minutes = 60
clock_hours = 12
step_minutes = clock_degrees / clock_minutes # = 6
step_hours = clock_degrees / clock_hours # = 30
angle_hours = hour * step_hours
angle_minutes = minute * step_minutes - ((step_hours / clock_minutes) * minute)
clock_angle = 360 - (max(angle_hours, angle_minutes) - min(angle_hours, angle_minutes))
return clock_angle
def main():
"""Runs the program"""
while True:
try:
user_input = input("Give a time (hh:mm)\n >> ")
if user_input.lower() == "exit" or user_input.lower() == "quit" or user_input.lower() == "q":
print("Thank you for using this program!")
break
hours = int(user_input.split(":")[0])
time = int(user_input.split(":")[1])
clock_angle = get_clock_angle(hours, time)
print(clock_angle)
print("")
except SystemExit as err:
main()
except ValueError as err:
print("Your values were not valid numbers. Please try again.\n")
main()
except IndexError as err:
print("Time format was not valid. Give times like this: 12:00.\n")
main()
except:
raise
print("Type 'exit' to exit from this program") # Has to be here so we won't get it everytime.
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment