Codecoach[military time] problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Codecoach[military time] problem

s=input().split() p=s[1] s=s[0].split(':') if(p=='PM' and int(s[0])==12): print('00'+':'+s[1]) elif(p=='PM'): print(str(int(s[0])+12)+':'+s[1]) else: print("%02d:%02d" %(int(s[0]),int(s[1]))) Share any other ways to do it

30th Dec 2019, 5:32 PM
Geek
Geek - avatar
10 Answers
+ 5
I did not test your code but I would say your output format is wrong. Output: XX:XX 01:45 instead of 1:45.
30th Dec 2019, 5:42 PM
Denise Roßberg
Denise Roßberg - avatar
+ 4
Geek it has already been asked, please use the searchbar next time
30th Dec 2019, 6:37 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
30th Dec 2019, 5:55 PM
Noob
Noob - avatar
30th Dec 2019, 5:57 PM
Anton Böhler
Anton Böhler - avatar
+ 1
Noob , technically speaking... 12:XX PM would mean the afternoon so the 24 hour time format of 12:XX would be correct. At one minute before midnight (11:59 PM) the time would be 23:59 and at the stroke of midnight (12:00 AM) the new day starts, so 24 hour time format would be 00:00. In denoting military time (in USA) it is also correct to omit the colon and abbreviate hours as HRS. Example: All below are valid notations of the same time. • 11:59 PM • 23:59 • 2359 HRS
31st Dec 2019, 11:30 PM
Rob 🇺🇸
Rob 🇺🇸 - avatar
0
print("%02d:%02d" %(int(s[0]),int(s[1]))) is there any other way to do it?
30th Dec 2019, 5:50 PM
Geek
Geek - avatar
0
time = str(input()) h = time[0:2] c = time[2] m = time[3:5] t = time[6::] if t == "AM": print(f'{h}{c}{m}') elif t == "PM": print(f'{int(h)+12}{c}{m}')
15th Dec 2021, 1:02 AM
Samnith Vath
Samnith Vath - avatar
0
time = input() x = time.split(':') pref = int(x[0]) for i in x[1]: if i == 'P' and pref !=12: pref +=12 elif i == 'A' and pref <11: pref = str(pref).zfill(2) elif i == 'A' and pref == 12: pref = (f'00') print(f'{pref}:{x[1][0:2]}')
18th Jan 2022, 1:07 AM
eli_learns_python
eli_learns_python - avatar
0
here is mine, ithe code like of human-ish way, and sorry for bad variable time = input().upper() if time.endswith('AM'): c = time.index('A') print(time[:c]) if time.endswith('PM'): a = time.index(':') d = time.index('P') b = str(abs(int(time[:a])+12)) print (b+time[a:d])
16th Jul 2022, 3:50 PM
Raihan Dwi Safreza
Raihan Dwi Safreza - avatar
0
Try this :) def hrstime24(x): if x[-2:] == 'AM': return x[:-2] elif x[-2:] == 'PM': return str(int(x[:2])+12)+x[2:5] print(hrstime24(input()))
22nd Nov 2022, 2:01 PM
Ming
Ming - avatar