0
My code is not giving correct OUTPUT for test case 3 and 5. My code is in comment section.
You want to convert the time from a 12 hour clock to a 24 hour clock. If you are given the time on a 12 hour clock, you should output the time as it would appear on a 24 hour clock. Task: Determine if the time you are given is AM or PM, then convert that value to the way that it would appear on a 24 hour clock. Input Format: A string that includes the time, then a space and the indicator for AM or PM. Output Format: A string that includes the time in a 24 hour format (XX:XX) Sample Input: 1:15 PM Sample Output: 13:15
4 Answers
+ 2
If input is 1:15 AM. You need to output 01:15.
But your code output 1:15.
+ 1
Suppose the input is 1:23 AM.
Your code outputs 1:23.
However the required output needs 4 digit format (XX:XX), so it should be 01:23.
0
def MilitaryTime(n) :
if n[-2:] == 'AM' :
if n[:2] == '12' :
return '00' + n[2:-2]
else :
return n[:-2]
if n[-2:] == 'PM' :
if n[:2] == '10' or n[:2] == '11' or n[:2] == '12' :
return str(int(n[:2]) + 12) + n[2:-2]
else:
return str(int(n[:1]) + 12) + n[1:-2]
n = str(input())
print(MilitaryTime(n))
0
Thank you Lisa CarrieForle .I got my mistake