Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中如何处理模0_Python_Modulo - Fatal编程技术网

python中如何处理模0

python中如何处理模0,python,modulo,Python,Modulo,我想创建一个代码,根据经过的小时数告诉我日期 我不想导入任何库,我只想自己计算。我希望代码能够灵活地工作,无论我给它的天数是多少,一天的小时数是多少,月数是多少。唯一的绝对值是小时,它总是一样的,有60分钟 下面的代码运行得非常好,但问题是我有13个0-12个月,而不是12个月,我不知道如何以最有效的方式处理0。下面的例子。我的小时数为0天0个月和2016年 dev = True # simple number of days in year snodiy = 365.25 # number

我想创建一个代码,根据经过的小时数告诉我日期

我不想导入任何库,我只想自己计算。我希望代码能够灵活地工作,无论我给它的天数是多少,一天的小时数是多少,月数是多少。唯一的绝对值是小时,它总是一样的,有60分钟

下面的代码运行得非常好,但问题是我有13个0-12个月,而不是12个月,我不知道如何以最有效的方式处理0。下面的例子。我的小时数为0天0个月和2016年

dev = True

# simple number of days in year
snodiy = 365.25
# number of hours in one day
nohiod = 24

# total hours passed
# 17672256 current year
thp = 17672257
dayc = 30  # number of days in a month #


# get date
def gd():
    year = (thp / snodiy) / nohiod
    rest = thp % (snodiy * nohiod)
    if dev:
        print("dev year rest:", rest)
    month = rest / (dayc * nohiod)
    rest %= (dayc * nohiod)
    if dev:
        print("dev month rest:", rest)
    day = rest / nohiod
    rest %= nohiod
    if dev:
        print("dev hour rest:", rest)
    hour = rest
    if dev:
        print("Year:", "%d" % year)
        print("Month:", "%d" % month)
        print("Day:", "%d" % day)
        print("Hour:", "%d" % hour)
    print("Hour:", "%d" % hour, "of", "%d" % day, "%d" % month, "%d" % year)

gd()
我将假设我们不受现实世界时间问题的所有约束,如闰年、闰秒、DST等。如果我们受约束,您将承担一项极其重要的任务,正如其他人提到的,您最好将其留给图书馆。继续

问题的实质是你隐含的假设,一年正好是12个月。您将一年定义为365.25*24=8766小时。您将一个月定义为30*24=720小时。12个月等于720*12=8640小时。你的一年比12个月长,所以在计算中得到13个月也就不足为奇了

你训练过度了。您需要做以下三件事之一:

dayc=snody/12 snodiy=dayc*12 接受并能够处理一年中不到12个月的事实。
不使用库计算日期是个坏主意。并非所有的小时都包含60分钟,例如闰秒,有些天在夏季轮班时有25小时,等等。日历库试图对您隐藏所有这些可怕的东西:我建议您使用日历库。您不能在打印时将数据增加1吗?Nf4r会给我带来1-13个问题您的月份变量应该在[0,11]间隔内,否则会出现问题。也就是说,只有12个月,其他的都是一个月error@glidbud这是对的。既然你在写自己的时间图书馆,我建议你在剩下的日子里从哈比人党那里借用这个技巧!