Militry time converting 12 hour to 24 hour, | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Militry time converting 12 hour to 24 hour,

x=input().strip() time=x.split() hour=time[0].split(':') if time[1]== 'AM': if hour[0]==12: print(f'00:{hour[1]}') else: print(f'{hour[0]}:{hour[1]}') else: h=int(hour[0])+12 print(f'{h}:{hour[1]}') Abovementioned code is what I try to convert 12 h to 24 h, but it didn't pass some test cases. I appreciate any help.

19th Apr 2021, 3:27 PM
Omid Amiri
Omid Amiri - avatar
2 Answers
+ 1
Omid Amiri This a problem in your code. If Input: 1:00 AM Output: 01:00 AM But your code didn't pass this condition. So it gives 1:00 AM. Alternate your code like this ↓↓↓↓ x=input().strip() time=x.split() hour=time[0].split(':') if time[1]== 'AM': if hour[0]==12: print(f'00:{hour[1]}') else: if len(hour[0])>1: print(f'{hour[0]}:{hour[1]}') else : print(f'0{hour[0]}:{hour[1]}') else: h=int(hour[0])+12 print(f'{h}:{hour[1]}')
19th Apr 2021, 3:51 PM
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜
˜”*°•.˜”*°• Mohan 333 •°*”˜.•°*”˜ - avatar
0
Thanks indeed
19th Apr 2021, 4:04 PM
Omid Amiri
Omid Amiri - avatar