Python中将小时分时间转换为文本格式的程序
假设我们有两个输入小时和分钟。我们必须以文本格式显示时间。这就像 -
8:00 : 8点
8:01 : 八点零一分
8:10 : 八点十分
8:15:八点一刻
8:30 : 八点半
8:40:二十分钟到九点
8:45:九点一刻
8:47:13 分钟到 9
8:28:八点二十八分
所以,如果输入像 h = 9, m = 42,那么输出将是十八分钟到十分钟
示例
让我们看看以下实现以获得更好的理解 -
def solve(h, m):text=["one", "two", "three", "four", "five", "six", "seven", "eight","nine","ten", "eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen","seventeen", "eighteen", "nineteen", "twenty", "twenty-one","twenty-two", "twenty-three", "twenty-four", "twentyfive"," twenty-six", "twenty-seven", "twenty-eight", "twenty-nine", "half"]
op=""
if (m == 0):
op = text[h - 1] + " o' clock"
elif (m == 30):
op = text[m - 1]+ " past " + text[h - 1]
elif (m == 1):
op = text[m - 1] + " minute past " + text[h - 1]
elif (m == 15):
op = text[m - 1]+ " past " + text[h - 1]
elif (m < 30):
op = text[m - 1] + " minutes past " + text[h - 1]
elif (m==45):
op = "quarter to " + text[h]
else:
op = text[(60 - m)-1] + " minutes to " + text[h]
return op
h = 9
m = 42
print(solve(h, m))
输入
9, 42输出结果
eighteen minutes to ten
以上是 Python中将小时分时间转换为文本格式的程序 的全部内容, 来源链接: utcz.com/z/331683.html