Military Time Challenge #test_case_4 | Sololearn: Learn to code for FREE!

+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!!

11/26/2021 1:12:44 PM

Ramprasad

17 Answers

New Answer

+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}")

+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.

+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)

+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.

+1

SoloProg but can u tell me what might be wrong with my code?

+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

+1

SoloProg doesnt work buddy

+1

SoloProg i guess using module is the only way...

+1

Ramprasad you didn't get my point? You just have to add this pair in your dictionary as I mentioned above. "04":"16"

+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

+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

0

Does this answer your question? https://www.sololearn.com/discuss/2921762/?ref=app

0

Python Learner wow buddy... But how did u know?

0

Ramprasad you can read my answer on this post https://www.sololearn.com/discuss/2921762/?ref=app

0

Don't use libraries just see my code 🙂

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