在Python中创建ApproxTime

在Python中创建ApproxTime,python,Python,在我的练习中,我遇到了如何创建函数的问题 编写一个名为approxTime的函数,该函数以两个整数作为输入:一个1到12之间的整数;并在0和59之间输入一个整数。函数应将时间打印到最近的四分之一 小时以下是一些例子: >>> approxTime(3, 18) The time is about quarter past three >>> approxTime(3, 28) The time is about half past three >&g

在我的练习中,我遇到了如何创建函数的问题

编写一个名为approxTime的函数,该函数以两个整数作为输入:一个1到12之间的整数;并在0和59之间输入一个整数。函数应将时间打印到最近的四分之一 小时以下是一些例子:

>>> approxTime(3, 18)
The time is about quarter past three

>>> approxTime(3, 28)
The time is about half past three

>>> approxTime(8,50)
The time is about quarter to nine

>>> approxTime(8,55)
The time is about nine o’clock
有什么办法吗

我用了另一种方式:

def approxTimehour(h):
        try:
            Hour = int(h)
        except:
            print("This is not a integer, please try again")
            return(None)
        if h<1:
            print("You cannot input Hour under 1")
            return(None)
        if h>12:
            print ("You cannot input Hour over 12")
            return(None)
        return Hour

def approxTimeMin(m):
        try:
            Minute = int(m)
        except:
            print("This is not a integer, please try again")
            return(None)
        if m<0:
            print("You cannot input Minute under 0")
            return(None)
        if m>59:
            print ("You cannot input Minute over 59")
            return(None)
        return Minute


PT1 = None
while PT1==None:
    h = int(input("Please input the hour (1-12):  "))
    PT1 = approxTimehour(h)

print("")

PT2 = None
while PT2==None:
    m = int(input("Please input the Minute (1-59):  "))
    PT2 = approxTimeMin(m)

print("The time is",h,":",m)

if m<15:
    print ("The time is ",h,"o'clock")
elif m>=15 and m<26:
    print ("The time is about quarter past",h)
elif m>25 and m<36:
    print ("The time is about half past",h)
elif m>35 and m<51:
    print ("The time is about quarter to",h+1)
elif m>50 and m<60:
    print ("The time is about",h+1,"o'clock")

我想知道如何以另一种方式做

少一点提示多一点解决方案:

def approxTime(hours,minutes):
    if hours>12 or hours<0:
        return "Function error, hour value outside range"
    if minutes>59 or minutes<0:
        return "Function error, minute value outside range"
    nums = {0:"twelve",1:"one",2:"two",
            3:"three",4:"four",5:"five",6:"six",
            7:"seven",8:"eight",9:"nine",10:"ten",
            11:"eleven",12:"twelve"}
    if hours==12:
        hours = 0
    if minutes>0 and minutes <8 :
        return "The time is about "+nums[hours]
    elif minutes>=8 and minutes<23:
        return "The time is about quarter past "+nums[hours]
    elif minutes>=23 and minutes<38:
        return "The time is about half past "+nums[hours]
    elif minutes>=38 and minutes<53:
        return "The time is about quarter to "+nums[hours+1]
    else:
        return "The time is about "+nums[hours+1]

另一种确定你也在哪一个时间0、15、30、45、60[点,四分之一,半点,四分之一到四分之一,点]的好方法是使用min来确定最接近的值:

closest = min([0, 15, 30, 45, 60], key=lambda x:abs(x-minute))
从这里开始,你应该做两件事:

确定是否需要为45和60添加一个小时,因为它们是四舍五入的

将数字解析为文本,这可以使用dicts轻松完成:

hours_text = {0:"zero",1:"one",2:"two",3:"three",4:"four",5:"five",6:"six",7:"seven",8:"eight",9:"nine",10:"ten",11:"eleven",12:"twelve"}
closest_text = {0: "o' clock", 15:"quarter past", 30: "half past", 45: "quarter to", 60: "o' clock"} 

使用这种方法,我可以在10行中编写函数。如果您感兴趣,您不想直接发布,这样您就可以自己尝试了。

如果您必须手工操作,您将如何处理此问题?首先写出逻辑。编程部分将把逻辑转换成代码。这类问题通常被认为过于宽泛/基于观点,不适用于SO。当你遇到问题时,我建议你尝试一下,问一些具体的问题。也许你可以看看这个:或者谢谢分享,但我想要的是提示而不是答案。想了解更多信息: