Military Time Challenge #test_case_4 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Military Time Challenge #test_case_4

I tried hard.. But cant meet the test_case_4 ... Worst of all i dont know what the case is... Heres my code x = {'1':'13', '2':'14', '3':'15', '4':'16', '5':'17', '6':'18', '7':'19', '8':'20', '9':'21', '10':'22', '11':'23', '12':'00'} m = input() c = m.split() k = list(str(c[0])) p = k.index(":") h ="" w ="" n ="" q ="" u ="" if "PM" in c: if p == 2: for t in k[:p]: h +=""+t del k[:2] if h in x: v = x.get(h) w +=""+v for t in k: w +=""+t print(w) elif p == 1: for t in k[:1]: h +=""+t del k[:1] if h in x: v = x.get(h) w +=""+v for t in k: w +=""+t print(w) elif "AM" in c: if p == 2: for t in k[:2]: q +=""+t del k[:2] if q == '12': f = x.get(q) u +=""+f for s in k: u +=""+s print(u) else: g = c.index("AM") del c[g] for s in c: n +=""+s print(n) if p == 1: g = c.index("AM") del c[g] for s in c: n +="0"+s print(n) Hope you can help!!

26th Nov 2021, 1:12 PM
Ramprasad
Ramprasad - avatar
16 Answers
+ 5
When you use libraries it makes the code easier from datetime import datetime t = input() mt = datetime.strptime(t, "%I:%M %p") print(f"{mt.hour:02}:{mt.minute:02}")
26th Nov 2021, 1:24 PM
SoloProg
SoloProg - avatar
+ 4
My advice is to slow down, and spend more time organizing the logic, and less typing 1. The dictionary is absolutely needless. You can see a clear relationship between keys and values. Code this relationship, not all cases individually. 2. There are way too many string operations. Think through, take time, and find more general processing which gets it done. By the way, what is the purpose of concatenating an empty string to variables? 3. Do use variables with meaningful names. You'll be surprised on how helpful they are when code grows. Typing code is a lot of fun - and highly addictive. But doing that without planning usually give frustrating results. Logic before, code after.
26th Nov 2021, 11:24 PM
Emerson Prado
Emerson Prado - avatar
+ 2
time_12 = input() if time_12.endswith('AM'): time_24 = time_12.replace(' AM','') print(time_24) else: time_12 = time_12.replace(' PM','') hr = time_12[:-3] min = time_12[2:] hr = int(hr)+12 time_24 = (f'{hr}{min}') print(time_24)
26th Nov 2021, 2:04 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
+ 2
I reliazed when you put the input without "AM": "1:15" This will be the output: "ValueError: 'AM' is not in list at g = c.index("AM")" I think that what you wrote in the second to last line it was correct : n +="0"+s Try my code now I fixed it.
26th Nov 2021, 3:29 PM
SoloProg
SoloProg - avatar
+ 1
SoloProg but can u tell me what might be wrong with my code?
26th Nov 2021, 1:28 PM
Ramprasad
Ramprasad - avatar
+ 1
I reliazed when you put the input: "1:15 AM" This will be the output: "01:15" The solution to your problem is in the second to last line it should be: n +=s #n +="0"+s
26th Nov 2021, 2:00 PM
SoloProg
SoloProg - avatar
+ 1
SoloProg doesnt work buddy
26th Nov 2021, 2:34 PM
Ramprasad
Ramprasad - avatar
+ 1
SoloProg i guess using module is the only way...
26th Nov 2021, 3:34 PM
Ramprasad
Ramprasad - avatar
+ 1
Ramprasad you didn't get my point? You just have to add this pair in your dictionary as I mentioned above. "04":"16"
26th Nov 2021, 4:00 PM
Python Learner
Python Learner - avatar
+ 1
a=input().split(':') d={i:str(12+i) for i in range(13)} if 'PM' in a[1]: a[0] = d[int(a[0])] elif '12' in a[0]: a[0] = '00' print(':'.join(a)[:-2]) #splitting #mapping #changing Hours if pm,am just need one change #joining without AmPm
27th Nov 2021, 6:51 AM
Prabhas Koya
+ 1
The codes that they put have many different solutions and I made a new solution as well. m = input() s = m.strip().replace(" ", ":").split(":") if s[2] == "PM": if s[0]=="12": print(f'{0:0<2}:{s[1]:0>2}') else: print(f'{int(s[0])+12}:{s[1]:0>2}') else: print(f'{s[0]:0>2}:{s[1]:0>2}') https://code.sololearn.com/ce9A91TBq7WF
27th Nov 2021, 2:26 PM
SoloProg
SoloProg - avatar
26th Nov 2021, 2:18 PM
Python Learner
Python Learner - avatar
0
JUMP_LINK__&&__Python__&&__JUMP_LINK Learner wow buddy... But how did u know?
27th Nov 2021, 3:42 AM
Ramprasad
Ramprasad - avatar
27th Nov 2021, 3:26 PM
Python Learner
Python Learner - avatar
0
Don't use libraries just see my code 🙂
27th Nov 2021, 3:33 PM
Pranav Hirani
Pranav Hirani - avatar
0
time=input() listTime=time.split(" ") if "AM" in listTime: time=listTime[0] else: listTime=listTime[0].split(":") hourPM=str(int(listTime[0])+12) time=":".join([hourPM,listTime[1]]) print(time) #This my answer
28th Nov 2021, 12:30 AM
hajar ait addi
hajar ait addi - avatar