fork download
  1. #Input the time
  2. input_time = input('What time is it?' )
  3.  
  4. #Split the string into hours and minutes
  5. time_hours, time_minutes = input_time.split(":")
  6.  
  7. #This will convert military hours to regular hours, and determine AM vs PM
  8. def HoursParser(hour):
  9. num_to_text={
  10. 0:"twelve",1:"one",2: "two",3: "three",
  11. 4: "four", 5: "five", 6: "six",
  12. 7: "seven", 8: "eight", 9: "nine",
  13. 10: "ten", 11: "eleven", 12: "twelve"
  14. }
  15. if hour>=12:
  16. am_or_pm = "pm"
  17. reg_time_hours = abs(hour-12)
  18. else:
  19. am_or_pm = "am"
  20. reg_time_hours = hour
  21. return(num_to_text[reg_time_hours],am_or_pm)
  22.  
  23. def MinutesParser(minutes):
  24. if int(minutes) < 20: #Handle all the weird cases (i.e., teens).
  25. min_to_text1_dict={
  26. 0:"",1:"oh one",2: "oh two",3: "oh three",
  27. 4: "oh four", 5: "oh five", 6: "oh six",
  28. 7: "oh seven", 8: "oh eight", 9: "oh nine",
  29. 10: "ten", 11: "eleven", 12: "twelve",
  30. 13: "thirteen", 14: "fourteen", 15: "fifteen",
  31. 16: "sixteen", 17: "seventeen", 18: "eighteen",
  32. 19: "nineteen"
  33. }
  34. return min_to_text1_dict[int(minutes)]
  35. if int(minutes) > 20: #Handle everything else that follows sane rules.
  36. min_tens_dict={
  37. "2":"twenty","3":"thirty","4":"forty","5":"fifty"
  38. }
  39. min_ones_dict={
  40. "0":"","1":"one","2": "two","3": "three",
  41. "4": "four", "5": "five", "6": "six",
  42. "7": "seven", "8": "eight", "9": "nine",
  43. }
  44. mins_together = min_tens_dict[minutes[:1]] + min_ones_dict[minutes[1:]]
  45. return mins_together
  46.  
  47. text_hours, am_pm = HoursParser(int(time_hours))
  48. text_minutes = MinutesParser(time_minutes)
  49. print("It's "+text_hours+" "+text_minutes+" "+am_pm)
Success #stdin #stdout 0.02s 27720KB
stdin
00:00
01:30
12:05
14:01
20:29
21:00
stdout
What time is it?It's twelve  am