+ 3

What's wrong in this code?

I tried solving military time code coach challenge! 3 of the 5 test were successful. 2 hidden tests were unsuccessful. This is the code: x = input() var = x.split(" ") var2 = var[0].split(":") if var[1].upper() == "PM": if var2[0] == "12": print ("12" + ":" + var2[1]) else: var3 = str(int(var2[0]) + 12 ) print (var3 + ":" + var2[1]) else : if var2[0] == "12": print ("00" + ":" + var2[1]) else: print (var[0]) What is the mistake? https://code.sololearn.com/cZv4mYo301nJ/?ref=app

25th Aug 2020, 2:29 AM
Hind Biswas
Hind Biswas - avatar
9 Réponses
+ 1
You just have to make it print ("0" + ":" + var2[1]) one zero instead of two and it will pass the cases
26th Aug 2020, 1:16 PM
Ruba Kh
Ruba Kh - avatar
25th Aug 2020, 3:50 AM
Namit Jain
Namit Jain - avatar
+ 4
Namit Jain and Brian thanks alot... BTW, Namit, what does zfill does?
25th Aug 2020, 3:56 AM
Hind Biswas
Hind Biswas - avatar
+ 4
Hind Biswas Or you can simply do: if var[0].index(":") == 1: print("0" + var[0]) else: print(var[0]) But doing this would be inefficient! Here zfill would be more efficient! Zfill does the same task! It checks that if the length of var[0] is less than 5 then it will add "0"s before it until it's length becomes 5 Like: a = "123" print(a.zfill(5)) Output: 00123
25th Aug 2020, 4:03 AM
Namit Jain
Namit Jain - avatar
+ 3
Brian I fixed that. Still not working..... I edited the code check it
25th Aug 2020, 2:49 AM
Hind Biswas
Hind Biswas - avatar
+ 3
Your correction is good. From other discussion questions I find that Code Coach needs a leading zero on the output of single-digit hours.
25th Aug 2020, 3:32 AM
Brian
Brian - avatar
+ 3
Is it working now? Or still facing the problem?
25th Aug 2020, 3:49 AM
Namit Jain
Namit Jain - avatar
+ 3
Ruba Kh thanks... I've already solved it.... Yet thanks for trying to help 😊
26th Aug 2020, 1:19 PM
Hind Biswas
Hind Biswas - avatar
+ 2
I tried the code and got a wrong answer for 12:40 pm.
25th Aug 2020, 2:47 AM
Brian
Brian - avatar