+ 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
2 Respuestas
+ 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)
+ 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)