+ 6

Military time challenge

I wrote this code for military time challenge and I don't understand it does not work for two test cases tform=input().split(":") if "PM" in tform[1] and tform[0] !="12": tform[0]=str(12+int(tform[0])) if tform[0]=="12" and "AM" in tform[1]: tform[0]="24" x,y=tform[0],tform[1] y=("".join(y))[:-3] tt=[x] tt.append(y) print(":".join(tt))

25th Dec 2019, 7:45 AM
Fabian Nwobi
Fabian Nwobi - avatar
34 Antworten
+ 17
I've test your code. 4:45 AM becomes 4:45 That's not ok for test cases. It should be 04:45 All hours between 00:xx and 09:xx must have two digits.
25th Dec 2019, 12:19 PM
Coding Cat
Coding Cat - avatar
+ 5
I'm still new to this, so after scanning around I noticed a couple examples that I think would maybe be a bit more efficient. But here is my shot anyway, and it worked. am_pm = input() if 'AM' in am_pm: print(am_pm[0:5]) else: x = am_pm[0:5].split(':') y = int(x[0]) + 12 z = str(y) + am_pm[2:5] print(z) Will gladly except criticism. In fact, I invite it. I want to get better. Also, I am a bit curious due to the fact that formatting the AM times in military for my code didn't require a 0 in front of the times before 10:00 AM. I say this because the most given reason in these forums for why a given military code attempt fails is the formatting of zeros in the x[0] index on times before 10:00 AM. Why did the method I chose not require the XX:XX format and others supposedly do??
12th Dec 2021, 1:51 AM
Stephen Norton
Stephen Norton - avatar
+ 3
Made it with only four lines time = input().split(':') if 'PM' in time[1]: time[0] = str((int(time[0])) + 12) print(time[0]+':'+ time[1][:-3])
2nd Jun 2022, 10:28 AM
Sator Arepo🌾🌌
Sator Arepo🌾🌌 - avatar
+ 2
Thanks Thomas. Exactly what I was looking for.
25th Dec 2019, 12:30 PM
Fabian Nwobi
Fabian Nwobi - avatar
30th Dec 2019, 1:07 PM
Noob
Noob - avatar
+ 2
I solved the problem with functional programming time = input() time = time.split() def am_converter(time): time = time[0].split(':') if int(time[0]) < 10: time = "0" + time[0] + ':' + time[1] elif int(time[0]) == 12 : time = '00' + ':' + time[1] else: time = time[0] + ':' + time[1] return time def pm_converter(time): time = time[0].split(':') if time[0] == '12': time = time[0] + ':' + time[1] else: time_start = (12 + int(time[0])) time_end = time[1] time = str(time_start) + ":" + time_end return time def military_time(time): if time[1] == "PM": print(pm_converter(time)) else: print(am_converter(time)) military_time(time)
5th Jan 2020, 4:26 AM
Corey Woolnough
Corey Woolnough - avatar
+ 2
time = str(input()).upper() h = time[0:2] c = time[2] m = time[3:5] t = time[6::] if t == "AM": print(f'{h}:{m}') elif t == "PM": print(f'{int(h)+12}:{m}')
15th Dec 2021, 7:58 AM
Samnith Vath
Samnith Vath - avatar
+ 2
My solution: import re time = input() pattern = "^\d+" number = (re.findall(pattern, time)) if re.search(r"PM", time): time = re.sub(pattern,str(int(number[0]) + 12),time) time = re.sub(r"AM|PM","",time) print(time)
17th Feb 2022, 8:52 PM
Przemysław Komański
Przemysław Komański - avatar
+ 1
https://code.sololearn.com/cGIeXNvMY073/?ref=app Thanks swim. I have done so, long before you mentioned it, but it still didn't work. I wish I knew all their criteria
25th Dec 2019, 8:22 AM
Fabian Nwobi
Fabian Nwobi - avatar
+ 1
Noob your solution doesnt handle 12pm or 12 am properly, here is a correction. (I like your use of replace and indexing) a=input() b=a.replace("AM","").replace ("PM","") ans="" if a[-2:]=="AM": if b[1]==":": print("0"+b[0:]) elif b[:2] == "12": print("00" + b[2:]) else: print(b[0:]) else: if b[1]==":": ans=str(int(b[0])+12)+b[1:] elif b[2]==":" and b[0:2]!="12": ans=str(int(b[0:2])+12)+b[2:] else: ans= b print(ans)
5th Jan 2020, 4:20 AM
Corey Woolnough
Corey Woolnough - avatar
+ 1
My solution: time = input() hour, pora = time.split(" ") hours, minutes = hour.split(":") def change(time): new_hours = "" if pora == "PM": new_hours = int(hours)+12 elif int(hours) <10: new_hours = "0"+hours else: new_hours = hours return(str(new_hours)+":"+minutes) print(change(time))
19th Jun 2020, 11:16 AM
Przemysław Papała
Przemysław Papała - avatar
4th Apr 2021, 9:23 AM
Naga Raju K
+ 1
https://code.sololearn.com/c1Riz7b6j8ma/?ref=app
28th Apr 2021, 7:47 AM
Mr.Dark
Mr.Dark - avatar
+ 1
Mr.Dark What about 12:00AM == 00:00
28th Apr 2021, 7:53 AM
Naga Raju K
+ 1
Thanks
28th Apr 2021, 7:56 AM
Mr.Dark
Mr.Dark - avatar
23rd Jul 2021, 4:36 AM
Johny Bracker
Johny Bracker - avatar
+ 1
PY: ------------------- def time_converter()->str: i:str = input() ampm:str = i[-2:] if ampm == "PM": h = i[:2] i=i.replace(h,str(int(h)+12)) print(i[:-3]) time_converter()
19th Sep 2021, 10:38 PM
_] | [\]
_] | [\] - avatar
+ 1
time = input().split() ampm = time[1] time12 = time[0][:-3] minutes = time[0][-3:] time24 = int(time12) + 12 if ampm == "PM" else time12 print(str(time24)+minutes)
10th Dec 2021, 10:01 PM
Mateo González Bufi
Mateo González Bufi - avatar
+ 1
a1=input() a=a1.split(':') if 'PM' in a1: a1=a1.replace(a[0],str(int(a[0])+12)) print(a1.replace(" PM","")) else: print(a1.replace(" AM",""))
2nd Jan 2022, 8:12 AM
Ayu Lestari Gunawan
+ 1
Coding Cat why does my code not need this (xx:xx) format for am times < 10?? I'm just getting into programming and after I solve a challenge, I always look it up to see if I did it comparably well to how others do it. I see the xx:xx format explanation given in this thread and in others on the same topic and I'm wondering out of genuine curiosity if mine is actually following that format? I'm wondering if they didn't write the tests wrong and my code really shouldn't be passing the tests. Because your format argument is absolutely valid. I posted it in this thread if you want to look at it.
2nd Jan 2022, 8:56 AM
Stephen Norton
Stephen Norton - avatar