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.'
if intext == '00:00':
return '12:00 a.m.'
hour = int(intext.split(':')) # get the hour as integer
minutes = intext.split(':') # get the minutes as is
return intext + ' p.m.'
return str(hour - 12) + ':' + minutes + ' p.m.'
return str(hour) + ':' + minutes + ' a.m.'
if __name__ == '__main__':
#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