Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Time Converter (24h to 12h) by gkozinakov
# Your task is to convert the time from the 24-h format into 12-h format by following the next rules:
# - the output format should be 'hh:mm a.m.' (for hours before midday) or 'hh:mm p.m.' (for hours after midday)
# - if hours is less than 10 - don't write a '0' before it. For example: '9:05 a.m.'
def time_converter(intext):
if intext == '00:00':
return '12:00 a.m.'
else:
hour = int(intext.split(':')[0]) # get the hour as integer
minutes = intext.split(':')[1] # get the minutes as is
if hour==12:
return intext + ' p.m.'
elif hour>12:
return str(hour - 12) + ':' + minutes + ' p.m.'
else:
return str(hour) + ':' + minutes + ' a.m.'
if __name__ == '__main__':
print("Example:")
print(time_converter('12:30'))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert time_converter('12:30') == '12:30 p.m.'
assert time_converter('09:00') == '9:00 a.m.'
assert time_converter('23:15') == '11:15 p.m.'
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 19, 2019
Comments: