0
Military Time ==[ Case#4 Failed ]==
This is my Military Time task testing 4 my code. My code output meet the requirements. But Failed in Case #4. Can anyone help me please! 🥲 And Sorry for my not good English. https://code.sololearn.com/cNrhWXgAsM8s/?ref=app
18 Answers
+ 5
EmptySpace his code works but more importantly is why isn't yours
+ 3
try Axel_ 1:00 PM
+ 2
EmptySpace
They are looking for a single input and a single output
Your code however requires no input and several lines of output
1:00 PM should be 13:00 remember to use split() with input()
+ 2
EmptySpace did you try my above comment?
It gives a key error for that kind of inputs. The reason is because there is no key that starts with 0 in the dictionary.
For example, it should return 16:45 for input 04:45 PM. Your code fails to pass this type of inputs. So, you have to add another key-value pairs that contain this thing.
(Spoiler: add this pair in your dictionary. "04:"16")
+ 2
JUMP_LINK__&&__Python__&&__JUMP_LINK Learner
Oh! Really thank you 🙂.
I forgot to write for XX:XX PM condition.
Thanks to all bros.
+ 2
t = input().split()
hm = t[0].split(":")
h = int(hm[0])
if t[1] == "PM":
h = 12 if h == 12 else h + 12
else:
h = 0 if h == 12 else h
print(f"{h:0>2}:{hm[1]}") # 0>2 pads to 2 digits with zeros
Done
+ 1
Does this answer your question?
https://www.sololearn.com/Discuss/2878582/?ref=app
But, we don't see any input variable in your code
+ 1
Try this:
a=input()
b=a.replace("AM","").replace ("PM","")
ans=""
if a[-2:]=="PM":
if b[1]==":":
ans=str(int(b[0])+12)+b[1:]
elif b[0:2]== "12":
ans="12"+b[2:]
else:
ans=str(int(b[0:2])+12)+b[2:]
else:
if b[1]==":":
ans="0"+b[0:]
elif b[0:2]=="12":
ans="00"+b[2:]
else:
ans=b
print(ans)
+ 1
EmptySpace did you try that code ?
+ 1
BroFar
Bro do you mean
Arun Jamson's code or my code?
+ 1
Don't complicate your code, as python was designed to write easy programs, try my code and you will see some tips :
string = input().split()
hour = string[0].split(":")
h = int(hour[0])
if string[1] == "PM" :
h += 12
print(("0" if h < 10 else ""), h, ":", hour[1], sep="")
+ 1
EmptySpace Your attempt too complex and you choose to check every condition, excessive IMHO.
I chose to treat inputs as simple as possible and work with hours only. I also could make it shorter, anyway take a look at my solution:
https://code.sololearn.com/cF4X0zYyp9Nw/?ref=app
+ 1
Lord_ your code would not pass
0
"""
< time_lst > is the user inputs for
testing my code output
"""
0
BroFar
Here's my actual code
import re
usr_time = input()
time = {
"1":"13","2":"14","3":"15",
"4":"16","5":"17","6":"18",
"7":"19","8":"20","9":"21",
"10":"22","11":"23","12":"00",
}
find = re.findall(r"\w{1,}", usr_time)
if find[2] == "AM" and find[0] == "12":
find[0] = time[find[0]]
if find[2] == "AM" and find[0] != "12":
if len(find[0]) == 1:
find[0] = "0" + find[0]
if find[2] == "PM" and find[0] == "12":
find[0] = find[0]
if find[2] == "PM" and find[0] != "12":
find[0] = time[find[0]]
find.insert(1, ":")
find.pop()
print("".join(find))
===============================
< time_lst > is the user inputs and for loop is for testing my code output from my linked code
0
BroFar
Bro please check my code and help me. I don't know how to pass the Test Case #4 according to my code.
PLEASE!
0
Hey folk why can’t you just write smth like that
https://code.sololearn.com/c2jO2gJY46GF/?ref=app
0
BroFar But I wrote this code when I was solving the Militaty Time task and it had passed…
Why do you think it would not pass? I am really interesed