+ 2
Why test cases 3 and 5 are failing in MILITARY TIME code coach problem?
I have done all I could with it, I also searched Google , the logic of the code is same on there as well ,that you have to take care of the 12s in AM and PM but still the test cases 3 and 5 are failing. Below is my code lst=input().split() new_lst=lst[0].split(':') if lst[1].lower()=='am': if new_lst[0]!='12': print(lst[0]) else: new_lst[0]='00' print(':'.join(new_lst)) else: if new_lst[0]!='12': new_lst[0]=str(int(new_lst[0])+12) print(':'.join(new_lst)) else: print(lst[0])
8 Answers
+ 8
I fixed the error in my code Aryan Prakash noticed.
https://code.sololearn.com/cTjMmDD1Ox1d
+ 9
Your approach works but if you use string formatting to add the leading zero(s) you need fewer lines.
t = input().split()
hm = t[0].split(":")
h = int(hm[0])
if t[1] == "PM":
h = 12 if h == 12 else h + 12
else:
h = 0 if h == 12 else h
print(f"{h:0>2}:{hm[1]}") # 0>2 pads to 2 digits with zeros
https://code.sololearn.com/cTjMmDD1Ox1d/?ref=app
+ 5
Aryan Prakash for input
4:30 AM
expected output should be 04:30 â
But your code outputs 4:30 â
This is what Ayush Rastogi is saying and you have still not corrected it in your code
https://code.sololearn.com/ck1Oagll59GJ/?ref=app
+ 5
Thanks Aryan Prakash good catch! I'll work on it.
+ 4
David
The string formatting approach is really nice.
But it would return 24:49 and 12:31 for 12:49 PM and 12:31 AM
+ 3
The most common error that happens is that when we print any hour less than 10 for am, we ignore the 0 before it.
The program will print 1:45, if 1:45 AM is given as input when it should print 01:45.
Just add 0 before every hour less than 10 for AM. It will get right. I think you will have to change the format of your program too to fit the change in.
+ 2
Thanks Arsenic and Ayush
Now, I understand it
+ 1
Ayush
I have done that (you can see that in the code part) but still it happens