+ 3

I need your help for the military time code

I can't figure out what is wrong and what cases 3 and 5 are. All the other cases work fine. Thank you for your help, I'm loosing my mind😅 https://code.sololearn.com/cOtjXpoom3Xv/?ref=app

3rd Sep 2022, 10:57 PM
Timo
2 Answers
+ 1
Timo Do not use replace function to replace numeric value because if value matches then each value will be replace. For example if input is 09:09 PM so if you do this: z=int(x[0:2])+12. x1=x.replace(x[0:2], str(z)) here 09 will be replace by (12 + 9) = 21 so output would be 21:21 which is wrong because it would be 21:09 Here is shortest solution of your code: x = input() isam = x.find("AM") hours = int(x[0:2]) mins = x[x.index(":"):-3] time = '' if isam > 0: time = x.replace("AM", "") else: time = str(hours + 12) + mins print (time)
4th Sep 2022, 6:35 AM
AÍąJ
AÍąJ - avatar
+ 1
I had this same issue when I solved it, it's just because there also needs to be a 0 before the hour if it's less than 10! (ex. 1:00 should be 01:00)
4th Sep 2022, 6:13 AM
Daniel C
Daniel C - avatar