+ 2

Military Time challenge

I am having some issues with this challenge. I am failing 2 of the 5 test cases, and I am not sure why. Here is my code: test = input() def military_time(time): time = time.upper() time = time.replace(':', ' ') time = time.split() time[0] = int(time[0]) time[1] = int(time[1]) if time[2] == 'AM' and time[0] == 12: hours = '00' elif time[2] == 'PM' and time[0] != 12: hours = time[0] + 12 else: hours = time[0] if time[1] == 0: minutes = '00' elif 0 < time[1] < 10: minutes = '0' + str(time[1]) else: minutes = time[1] return f'{hours}:{minutes}' print(military_time(test)) I used a for loop to plug in test values and they all seem to work out, but obviously I am overlooking something. If anyone wishes to help, it would be very welcomed!

9th Jun 2020, 7:48 PM
Will L
Will L - avatar
5 odpowiedzi
+ 3
For 1:15 AM it ouputs 1:15 but the output should be in format 01:15
9th Jun 2020, 7:51 PM
Abhay
Abhay - avatar
+ 2
Abhay That's what I was missing! Thank you very much
9th Jun 2020, 7:55 PM
Will L
Will L - avatar
+ 2
Just a small remarque here : it's easier to work with strings your code is too long
9th Jun 2020, 9:45 PM
Hisham YUM 🇲🇦
Hisham YUM 🇲🇦 - avatar
+ 2
Hey Hisham SH 🇲🇦 I took your advice and optimized my code after submitting it. I'm still learning, but I forgot that strings can be sliced like lists. That helped slim it down.
10th Jun 2020, 3:32 PM
Will L
Will L - avatar
+ 1
Yeah I hope this may help you Will L . We are here to learn 🤷🏻‍♂️ Just take it easy.
10th Jun 2020, 5:32 PM
Hisham YUM 🇲🇦
Hisham YUM 🇲🇦 - avatar