我想写一个时间格式化程序time_format(hours, minutes)
,它花费一天的时间,以小时和分钟表示,将其四舍五入到最接近的5分钟,并用英语单词表示。
我需要编写一个格式化输入的方法,例如
"9,1"
进入“九点钟”
time_format(9, 1) returns "nine o'clock"
time_format(9, 5) returns "five after nine"
time_format(9, 13) returns "quarter after nine"
time_format(9, 9) returns "ten after nine"
time_format(9, 20) returns "twenty after nine"
time_format(9, 24) returns "twenty five after nine"
time_format(9, 30) returns "half past nine"
time_format(9, 36) returns "twenty five to ten"
time_format(9, 38) returns "twenty to ten"
time_format(9, 45) returns "quarter to ten"
time_format(9, 50) returns "ten to ten"
time_format(9, 56) returns "five to ten"
time_format(12, 00) returns "noon"
time_format(15, 20) returns "twenty after three"
time_format(23, 45) returns "a quarter to midnight"
知道我怎么能这样做吗?我一直在看日期对象,但它没有给我一个方法来提供像20这样的数字并获得它的文字文本。
答案 0 :(得分:0)
好吧,现在,你问了一个想法,所以这是我朝着正确的方向努力。我认为你很喜欢使用字典。但是对于如何构造输出,第一个数字还是第二个数字更重要的是什么?
第二个!
但是字典对于第二种情况不起作用,如果是elif链则需要。这是我用来处理第二个值的代码的两个片段。从那里应该很容易,我希望!
def time_eval_thingy ( a, b ):
b = int( b ) # probably not needed, just to be sure
if b < 5:
outputstring = "SPECIAL"
elif b < 10:
outputstring = "5 after "
...
elif b < 50
outputstring = "Quarter until "
a += 1 # a is the hour, and increasing it here will make the hour appear higher in the dictionary call that occurs after the time when we decide the signifcance of b
我应该补充一点,后来我检查输出是“SPECIAL”,如果是,我将“O'clock”添加到末尾,表示值不是12或00(将整个字符串转换为中午或午夜)