python-checkio/ElectronicStation/time-converter-12h-to-24h.py

27 lines
873 B
Python

def time_converter(time):
[time, apm] = time.split(" ")
time_int = int(time.replace(':', ''))
if apm == "a.m.":
if time_int < 1200:
return time.rjust(5, "0")
else:
[hours, minutes] = time.split(":")
return "00:{}".format(minutes)
else:
if time_int < 1200:
[hours, minutes] = time.split(":")
return "{}:{}".format(int(hours) + 12, minutes)
else:
return time
if __name__ == '__main__':
print("Example:")
print(time_converter('12:30 p.m.'))
# These "asserts" using only for self-checking and not necessary for auto-testing
assert time_converter('12:30 p.m.') == '12:30'
assert time_converter('9:00 a.m.') == '09:00'
assert time_converter('11:15 p.m.') == '23:15'
print("Coding complete? Click 'Check' to earn cool rewards!")