+ 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])

10th Aug 2020, 6:18 AM
Aryan Prakash
Aryan Prakash - avatar
8 Respostas
+ 8
I fixed the error in my code Aryan Prakash noticed. https://code.sololearn.com/cTjMmDD1Ox1d
11th Aug 2020, 9:25 AM
David Ashton
David Ashton - avatar
+ 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
10th Aug 2020, 8:59 AM
David Ashton
David Ashton - avatar
+ 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
10th Aug 2020, 6:42 AM
Arsenic
Arsenic - avatar
+ 5
Thanks Aryan Prakash good catch! I'll work on it.
10th Aug 2020, 3:42 PM
David Ashton
David Ashton - avatar
+ 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
10th Aug 2020, 9:25 AM
Aryan Prakash
Aryan Prakash - avatar
+ 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.
10th Aug 2020, 6:25 AM
Ayush Rastogi
Ayush Rastogi - avatar
+ 2
Thanks Arsenic and Ayush Now, I understand it
10th Aug 2020, 7:01 AM
Aryan Prakash
Aryan Prakash - avatar
+ 1
Ayush I have done that (you can see that in the code part) but still it happens
10th Aug 2020, 6:28 AM
Aryan Prakash
Aryan Prakash - avatar