+ 2

Military time challenge in python

Hello, my code fails 2 of the hidden test cases and I'm not sure why, could anyone point me in the right direction? stri = input() stri = stri.split() num = stri[0] word = stri[1] # splits input into numerals and daypart, then assigns those to their own variables num = num.split(":") #splits hours and minutes at num[0] and num[1] repectively if word == "PM" and int(num[0]) != 12: output = int(num[0]) + 12 #adds 12 to hours if pm except for special case of 12pm, as thats still 12:00 in military time elif word == "AM" and int(num[0]) == 12: ouput = int(num[0]) - 12 #checks for special case of 12am, not sure what the expected output is, but if 00:00, then -12, if 24:00 then +12 else: output = int(num[0]) output = str(output) if len(output) == 1: ouput = "0" + output #converts to string and adds leading 0 if necessary print(f"{output}:{num[1]}")

19th Aug 2020, 7:30 PM
Danny Evans
Danny Evans - avatar
8 Respuestas
+ 7
You missed 't' in output: if len(output) == 1: output = "0" + output so was incorrect result for time like 3:15 AM
19th Aug 2020, 8:08 PM
Julia Shabanova
Julia Shabanova - avatar
+ 3
Oh my God, the missing t was it, fixed that and passed all 5 cases. Thank you both of you for all the help
19th Aug 2020, 8:20 PM
Danny Evans
Danny Evans - avatar
+ 1
First and foremost, Pandas is your friend. Secondly, have you built a use case for lower "am" and "pm"?... Those hidden test cases can be a bit tricky, so its hard to say what needs to be tweaked
19th Aug 2020, 7:35 PM
Steven M
Steven M - avatar
+ 1
Hmm, I'm pretty new and have never heard of pandas before but I'll look into it. Gonna try adding in code accounting for lowercase am/pm and will get back
19th Aug 2020, 7:38 PM
Danny Evans
Danny Evans - avatar
+ 1
Here you go, a couple of links to get you started, there is a lot of power in Pandas https://datatofish.com/strings-to-datetime-pandas/ datetime formatting https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
19th Aug 2020, 7:43 PM
Steven M
Steven M - avatar
+ 1
Used .lower() to guarantee they would be written as am/pm then adjusted my conditionals accordingly, still no dice. Guess I'll read up on pandas and then take another shot at it using that. Thanks for your help!
19th Aug 2020, 7:49 PM
Danny Evans
Danny Evans - avatar
+ 1
If you want to go with the code you have written, I would check line 23... it fails with 12:01 AM as an input
19th Aug 2020, 7:55 PM
Steven M
Steven M - avatar
+ 1
st=input() w=st[:-2].split(":") y=w[0] x=w[1] if(len(y)<2): y="0"+y if(st[-2]=='A'): if(y=="12"): y="00" print(str(y)+":"+x) else: fin=0 if(y!="12"): fin=int(y)+12 fin=str(fin) print(str(fin)+":"+x)
21st Aug 2020, 7:14 AM
Mahesh Kantariya