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.
2 ответов
+ 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]}')
0
Thanks indeed