Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4
<CODE KING/> when I entered 12:23 PM your program wrongly printed 00:23. As a "Code King", it is apparent that you love writing quantities of code. Maybe think about how to have the computer do more of the work. A 147-line program is about 50 times larger than is needed to solve this task. Really. A dozen lines at most, and even two or three lines will do it.
6th Dec 2021, 9:25 PM
Brian
Brian - avatar
+ 3
# Military Time - Code Coach from datetime import datetime t = input() mt = datetime.strptime(t, "%I:%M %p") print(f"{mt.hour:02}:{mt.minute:02}") # or m = input() s = m.strip().replace(" ", ":").split(":") if s[2] == "PM": if s[0]=="12": print(f'12:{s[1]:0>2}') else: print(f'{int(s[0])+12}:{s[1]:0>2}') else: if s[0]=="12": print(f'00:{s[1]:0>2}') else: print(f'{s[0]:0>2}:{s[1]:0>2}') # Keep learning & happy coding :D https://www.sololearn.com/Codes?ordering=MostRecent&query=Military%20Time https://en.wikipedia.org/wiki/24-hour_clock
6th Dec 2021, 8:44 PM
SoloProg
SoloProg - avatar
+ 3
https://code.sololearn.com/cqI0MkUWeGG0/?ref=app This is more simpler than ur long code...
7th Dec 2021, 4:37 AM
Pallavi Bhardwaj
Pallavi Bhardwaj - avatar
+ 3
<CODE KING/> the code looks much better!! I have a couple corrections to fix the print() string for 12:xx AM and 12:xx PM. It should be: if choice == "PM": if hours == "12": print(f"{hours}:{minutes}") . . . else: if hours == "12": print(f"00:{minutes}") . . . Also I believe it needs to add a leading zero for single-digit hours. See if you can understand how the sample codes by SoloProg do that. (hint: formatted strings).
7th Dec 2021, 6:32 AM
Brian
Brian - avatar
+ 2
<CODE KING/> verily, it passes! 😃👍👏👏👏 Here are my own couple of successful codes for your reading enjoyment: #input format is [h]h:mm <A|P>M hr, mm, meridian = input().replace(' ', ':').split(':', 2) #convert hr and replace 12 with 0 hr = int(hr)%12 #make 24hr if meridian=="PM": hr += 12 #format hour with filler zero print(f"{hr:02}:{mm}") And a shorter revision: #mm_M holds both, minutes and Meridian hr, mm_M = input().split(':', 1) #12 AM is 00. For PM hours add 12. hr = int(hr)%12 + 12*(mm_M[-2]=='P') #format hour with filler zero #show minutes without meridian print(f"{hr:02}:{mm_M[:2]}")
7th Dec 2021, 7:58 AM
Brian
Brian - avatar