+ 2

Code Coach "MILITARY TIME"

inp = str(input()) pisah = [] pm = "PM" duabelas = '12' if pm in inp: if duabelas in inp: for x in inp.split(" "): pisah.append(x) print(pisah[0]) else: for x in inp.split(":"): pisah.append(x) a = ' '.join(pisah) b = a.split(" ") c = int(b[0]) + 12 pisah.append(str(c)) print(b[1] + ":" + b[2]) else: if duabelas in inp: for x in inp.split(":"): pisah.append(x) a = ' '.join(pisah) b = a.split(" ") c = int(b[0]) - 12 pisah.append(str(c)) print(b[1] + ":" + b[2]) else: for x in inp.split(" "): pisah.append(x) print(pisah[0]) From that code, why I got wrong at Test Case #3 and Test Case #5. Even that is manual method, but I think its right.

2nd Apr 2021, 6:57 AM
Malik Hafid Sidiq
Malik Hafid Sidiq - avatar
43 Antworten
+ 2
I don't fully not understanding your logic.. but just change your original code to output If hour ==12 or 1<=hour<=9 : print("0+hour") But you have lot redundant code try to remove and write only once outside if , if you doing same in both (if, else) parts. for jam in splitspasi: hasil.append(jam) print(hasil[0]) is just same as splitsasi[0]. (I think) What if input is 1:11 AM (seconds is 12,, 11, or 10 ) Output is 1:11 It is true for "11" in splitspasi, accidentally lead wrong by your logic. Malik Hafid Sidiq Simple steps: Take input and split with space if list[1] is AM Do hr%12 , if below 10 append 0. if list[1] is PM, if hr!=12, hr+12, else 12 You can go with your logic. just try to append 0 for input 12, and 1-9 AM hours...
2nd Apr 2021, 3:03 PM
Jayakrishna 🇮🇳
+ 3
Malik Hafid Sidiq Tested these sample cases , getting wrong.. Input 12:00 AM => output need is 00:00 1:00 AM => 01:00 All you need is hr is in double digits if it is in single digit..
2nd Apr 2021, 1:14 PM
Jayakrishna 🇮🇳
+ 1
Sorry, I am noob in python and this SoloLearn
2nd Apr 2021, 7:22 AM
Malik Hafid Sidiq
Malik Hafid Sidiq - avatar
+ 1
okay, so i would start off by splitting the time up with the “hello”.split(“e”) method to get “12” “00” and “AM” (using the method twice). second after doing the neccesary math, you can turn it back into double digits by using “%02d” % integer which pads the number with zeroes (two in this case, hence the two). The D stands for digit, and the % is the special formatter operator in this case (if we were working just with numbers and not a string, it would be modulus).
2nd Apr 2021, 1:21 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
Sorry Im very newbie in python, so I still use logic
2nd Apr 2021, 1:23 PM
Malik Hafid Sidiq
Malik Hafid Sidiq - avatar
+ 1
hasil.append(jam) gabunglagi = ' '.join(hasil) pisahlagi = gabunglagi.split(" ") konvert = "0" + pisahlagi[0] print(konvert + ":" + pisahlagi[1])
2nd Apr 2021, 1:32 PM
Malik Hafid Sidiq
Malik Hafid Sidiq - avatar
2nd Apr 2021, 2:43 PM
Malik Hafid Sidiq
Malik Hafid Sidiq - avatar
+ 1
This is an answer to the code coach. I put comments throughout to explain it. READ AND UNDERSTAND BEFORE USING. https://code.sololearn.com/ckyTBARScyEA/?ref=app
2nd Apr 2021, 3:34 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
Calvin Thomas Umm... that’s more conpact but almost unreadable. besides, you’re not supposed to post code answers with no explanation.
3rd Apr 2021, 1:24 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
David Ordás the lesson is meant to teach about string formatting, not the datetime module.
4th Apr 2021, 5:31 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
+ 1
Try this.. t=input().strip() numt,ch=t.split(' ') hour,minute=numt.split(':') hour=int(hour) if ch=='AM': if hour<10: print("0{}:{}".format(str(hour),str(minute))) else: print("{}:{}".format(str(hour),minute )) elif ch=='PM': if hour==12: print("00:{}".format(str(minute))) else: print("{}:{}".format(str(hour%12+12),minute))
4th Apr 2021, 6:04 AM
Amrish Kumar
Amrish Kumar - avatar
+ 1
David Ordás I am a newbie in python, so I just using a logic to solve the problem. Thanks for you sugestion..
4th Apr 2021, 6:26 AM
Malik Hafid Sidiq
Malik Hafid Sidiq - avatar
0
for future reference, please use code playground to share code. this code looks unneccesarily complicated, like you are trying to deal with everything in the form of strings, or lists, or anything but integers.
2nd Apr 2021, 7:09 AM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
are you using the app?
2nd Apr 2021, 12:59 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
Who gave you this assignment?
2nd Apr 2021, 1:26 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
i forgot to mention using the int(“42”) function to turn them into numbers.
2nd Apr 2021, 1:26 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
It is in medium Code Coach I was try from easy until medium using a nub method But this task is already by my self You will get headache if read my code.... Beloww
2nd Apr 2021, 1:30 PM
Malik Hafid Sidiq
Malik Hafid Sidiq - avatar
0
inp = str(input()) splitspasi = inp.split(" ") splittitik = inp.split(":") hasil = [] if "PM" in inp: if "12" in splittitik: for jam in splitspasi: hasil.append(jam) print(hasil[0]) else: for jam in splittitik: hasil.append(jam) gabunglagi = ' '.join(hasil) pisahlagi = gabunglagi.split(" ") konvert = int(pisahlagi[0]) + 12 print(str(konvert) + ":" + pisahlagi[1]) else: if "12" in splittitik: for jam in splittitik: hasil.append(jam) gabunglagi = ' '.join(hasil) pisahlagi = gabunglagi.split(" ") konvert = int(pisahlagi[0]) - 12 print(str(konvert) + "0" + ":" + pisahlagi[1]) elif "10" in splittitik: for jam in splitspasi: hasil.append(jam) print(hasil[0]) elif "11" in splittitik: for jam in splitspasi: hasil.append(jam) print(hasil[0]) else: for jam in splittitik:
2nd Apr 2021, 1:31 PM
Malik Hafid Sidiq
Malik Hafid Sidiq - avatar
0
PLEASE use Code Playground (in the code bits tab) to share code!!!
2nd Apr 2021, 1:34 PM
Wilbur Jaywright
Wilbur Jaywright - avatar
0
But how???
2nd Apr 2021, 1:41 PM
Malik Hafid Sidiq
Malik Hafid Sidiq - avatar