+ 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
9 Respostas
+ 1
You just have to make it
print ("0" + ":" + var2[1])
one zero instead of two and it will pass the cases
+ 4
+ 4
Namit Jain and Brian thanks alot...
BTW, Namit, what does zfill does?
+ 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
+ 3
Brian I fixed that. Still not working..... I edited the code check it
+ 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.
+ 3
Is it working now?
Or still facing the problem?
+ 3
Ruba Kh thanks... I've already solved it.... Yet thanks for trying to help 😊
+ 2
I tried the code and got a wrong answer for 12:40 pm.