本文介绍了0-23小时军事时钟到标准时间(hh:mm)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将如何进行此过程?假设有人输入 3:5,我应该可以得到输出 3:05 AM。或者,如果有人输入 00:00,我应该得到 12:00 AM。如果将时间格式简单地输入为 hhmm,我就会意识到有很多解决方法,但是输入的范围可以是 h:m, hh:m, hh:m和 hh:毫米。
How would I go about this process? Let's say that someone types in "3:5", and I should be able to get the output "3:05 AM". Or if someone types in "00:00", I should get "12:00 AM". I realize the plethora of ways to go about this if the time format was simply input as "hhmm", but the input can range from "h:m", "hh:m", "hh:m", and "hh:mm". So it's very situational.
def timeConvert():
miliTime = int(input("Enter a time in hh:mm (military) format: "))
miliTime.split(":")
if len(miliTime) == 3:
hours = miliTime[0]
minutes = miliTime[2]
if hours < 0:
print("Hours can't be less than 0.")
elif len(miliTime) == 4:
if miliTime[0:2] >= 10:
hours = militime[0:2]
minutes = militime[3]
if minutes < 0:
print("Minutes can't be less than 0.")
if minutes < 10:
minutes = 0 + minutes
else:
hours = miliTime[0]
minutes = militime[2:]
if minutes >= 60:
print("Too big of a number for minutes.")
else:
hours = miliTime[0:2]
minutes = miliTime[3:]
setting = AM
if hours > 12:
setting = PM
hours -= 12
print(hours + ":" + minutes + setting)
timeConvert()
推荐答案
用分隔输入:
,并最终使用格式化的输出:
Split the input by :
, and use formatted output in the end:
def timeConvert():
miliTime = input("Enter a time in hh:mm (military) format: ")
hours, minutes = miliTime.split(":")
hours, minutes = int(hours), int(minutes)
setting = "AM"
if hours > 12:
setting = "PM"
hours -= 12
print(("%02d:%02d" + setting) % (hours, minutes))
这篇关于0-23小时军事时钟到标准时间(hh:mm)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!