Skip to content

Instantly share code, notes, and snippets.

@MichaelSDavid
Last active September 4, 2020 22:57
Show Gist options
  • Save MichaelSDavid/ae4afb8607a0dd7d549af42720de3789 to your computer and use it in GitHub Desktop.
Save MichaelSDavid/ae4afb8607a0dd7d549af42720de3789 to your computer and use it in GitHub Desktop.
SoloLearn Code Coach solution for Military Time in Python
sample_string = input()
def military_time(twelvehour_str):
if "PM" in twelvehour_str:
if len(twelvehour_str) == 8:
initial_hour = twelvehour_str[:2]
new_hour = int(initial_hour) + 12
final24_str = str(new_hour) + twelvehour_str[2:5]
else:
initial_hour = twelvehour_str[0]
new_hour = int(initial_hour) + 12
final24_str = str(new_hour) + twelvehour_str[1:5]
else:
if len(twelvehour_str) == 8:
final24_str = twelvehour_str[:5]
else:
final24_str = "0" + twelvehour_str[:5]
print(final24_str)
military_time(sample_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment