Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_time(prompt, maxhr):
- while True:
- sin = input("\n" + prompt)
- sinsplit = sin.split(':')
- if len(sinsplit) != 2:
- print("Invalid")
- continue
- if not (sinsplit[0] + sinsplit[1]).isdecimal():
- print("Invalid")
- continue
- h = int(sinsplit[0])
- m = int(sinsplit[1])
- if h > maxhr or m > 59:
- print("Invalid")
- continue
- break
- return h, m
- while True:
- hh, mm = get_time("Enter start time as hh:mm in 12-hour clock format: ", 12)
- while True:
- ap = input("Enter 'am' or 'pm': ").lower()
- if ap in ["am", "pm"]:
- break
- print("Invalid")
- if hh == 0 and ap == "pm":
- print("Invalid")
- continue
- break
- # Convert start time to 24-hour format
- if ap == "pm" and hh < 12:
- hh += 12
- if ap == "am" and hh == 12:
- hh = 0
- print(f"Start time is: {hh:02d}:{mm:02d}")
- dh, dm = get_time("Enter duration as hh:mm (max 24 hours): ", 23)
- mm += dm
- hplus, mm = divmod(mm, 60)
- hh += dh
- hh += hplus
- ap = "am"
- hh = hh % 24
- if hh > 11:
- ap = "pm"
- if hh > 12:
- hh -= 12
- if hh == 0:
- hh = 12
- print(f"Finish time is {hh:02d}:{mm:02d} {ap}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement