这个练习在Thinkpython 2e中意味着什么?

时间:2016-12-06 14:55:15

标签: python

  

时间模块提供一个函数,也称为时间,它在“纪元”中返回当前的格林威治标准时间,这是一个用作参考的任意时间   在UNIX系统上,时代是1970年1月1日。

> import time 
> time.time()
1437746094.5735958
  

编写一个脚本,读取当前时间并将其转换为一小时,几分钟和几秒的时间,再加上自纪元以来的天数。

我没看到这个练习如何连接到章节5.Conditionals和Recursion以及如何编写代码来实现这一点?

想回答我的问题。 所以,作为你的建议,我写了一段这样的代码:

import time

secs = time.time()

def time():
    mins = secs / 60
    hours = mins / 60
    days = hours/24
    print 'The minues:',mins,'The hours:' ,hours, 'The days:',days

print 'The seconds:', secs, time()

它输出如下结果: 秒数:1481077157.6最低限度:24684619.2933小时:411410.321554天数:17142.0967314 我的问题是“无”来自哪里?

4 个答案:

答案 0 :(得分:0)

import time

def current_time():
    current=time.time()
    t_sec = current % 86400
    c_hours = int(t_sec/3600)
    t_minutes = int(t_sec/60)
    c_mins = t_minutes % 60
    c_sec = int(t_sec % 60)

    days=int(current/86400)

    print("The Current time is",c_hours,':',c_mins,':',c_sec)
    print('Days since epoch:', days)

答案 1 :(得分:0)

>import time
>epoch=time.time()

>#60*60*24=86400
>total_sec = epoch % 86400
>#60*60
>hours = int(total_sec/3600)
>total_minutes = int(total_sec/60)
>mins = total_minutes % 60
>sec = int(total_sec % 60)

>days=int(epoch/86400)

>print("The Current time is",hours,':',mins,':',sec)
>print("Days since epoch:", days)

答案 2 :(得分:0)

编辑以回应:“我的问题是“无人”来自哪里?”

在最后的打印函数中,您调用了“ time()”,但没有返回任何内容,因此它打印了“ None”。 如果您希望“无”消失,请尝试以下操作:

import time

secs = time.time()

def time():
  mins = secs / 60
  hours = mins / 60
  days = hours/24
  print ('The minues:',mins,'The hours:' ,hours, 'The days:',days)

time()
print ('The seconds:', secs)

重点应该是,如果要使用递归函数,则应返回一些内容,然后将其用于计算。


让我们看一下练习说明:

编写一个脚本,该脚本读取当前时间并将其转换为一天中的时间(以小时,分钟和秒为单位),以及自该纪元以来的天数。

我对问题的理解是,答案的格式应如下所示:

Today is 18 hours, 12 minutes, 11 seconds and 18404 days since epoch. 

要获得此答案,可以使用带有“模运算符”的函数,该函数是第5.1节的一部分。然后,您可以用天数,小时,分钟和秒减去包含“ today”的变量。这有点递归,可以帮助您理解后续练习。

答案 3 :(得分:0)

import time

#the epoch time
epoch = int(time.time())

#calculate number of days since epoch
days = epoch / (60 * 60 * 24)
hour = days % int(days) * 24
min  = hour % int(hour) * 60
sec  = min % int(min) * 60

print(f"Days since epoch: {int(days)}\nCurrent Time: {int(hour)}:{int(min)}:{int(sec)}")