python-datetime 输出 -1 天而不是所需的输出

python-datetime outputting -1 day instead of desired output

所以,我的目标是更改日期时间的输出:

time left: -1 day, 23:57:28

为此:

time left: 0:00:30

现在,这需要是动态的,因为代码应该在字典中更改。我想弄清楚为什么它输出

-1 day, 23:57:28

我试过移动它执行的位置,甚至更改一些其他代码。我只是不明白为什么它显示为 -1 天。似乎它执行了太多次

另外,附带说明一下,该程序的目的是计算出在给定时间限制的情况下可以将多少首歌曲放入播放列表。我似乎无法找出正确的 if 语句来使其工作。有人也可以帮忙吗?

这是程序的当前输出:

0:02:34
0:06:30
Reached limit of 0:07:00
time left: -1 day, 23:57:28

查看下面的代码:

import datetime

#durations and names of songs are inputted here
timeDict = {
    'Song1' : '2:34',
    'Song2' : '3:56',
    'Song3' : '3:02'
}


def timeAdder():

   #assigns sum to the datetime library's timedelta class
    sum = datetime.timedelta()

    #sets the limit, can be whatever
    limit = '0:07:00'

    #calculates the sum
    for i in timeDict.values():
         (m, s) = i.split(':')
         d = datetime.timedelta(minutes=int(m), seconds=int(s))
         sum += d

       #checks whether the limit has been reached
         while str(sum)<limit:
             print(sum)
             break

        
         
         
         #commits the big STOP when limit is reached
         if str(sum)>limit:
            print("Reached limit of " + limit)
            break

    #timeLeft variable created as well as datetime object conversion to a string       
    x = '%H:%M:%S'
    timeLeft = datetime.datetime.strptime(limit, x) - datetime.datetime.strptime(str(sum), x)

    for i in timeDict:
        if timeDict[i] <= str(timeLeft): 
            print("You can fit " + i + " into your playlist.")
    print("time left: " + str(timeLeft))
            

def main():
    timeAdder()
    
main()

如有任何帮助,我们将不胜感激。

It seems likes it is executing one too many times

宾果游戏。问题在这里:

sum += d

...

#commits the big STOP when limit is reached
if str(sum)>limit:
  print("Reached limit of " + limit)
  break

您正在立即添加您的总和,然后检查它是否已超过限制。相反,您需要在实际添加之前检查添加到总和是否会超过限制

另外两件事:首先,sum 是一个 Python 关键字,因此您不想将它用作变量名。其次,你永远不想将数据作为字符串进行比较,你会得到奇怪的行为。喜欢:

>>> "0:07:30" > "2:34"
False

所以你所有的时间都应该是timedelta个对象。

这是新代码:

def timeAdder():
   #assigns sum to the datetime library's timedelta class
    sum_ = datetime.timedelta()

    #sets the limit, can be whatever
    limit = '0:07:00'
    (h, m, s) = (int(i) for i in limit.split(":"))
    limitDelta = datetime.timedelta(hours=h, minutes=m, seconds=s)

    #calculates the sum
    for i in timeDict.values():
         (m, s) = i.split(':')
         d = datetime.timedelta(minutes=int(m), seconds=int(s))

         if (sum_ + d) > limitDelta:
            print("Reached limit of " + limit)
            break
         
         # else, loop continues
         sum_ += d
         print(sum_)
     
    timeLeft = limitDelta - sum_

    for songName, songLength in timeDict.items():
        (m, s) = (int(i) for i in songLength.split(':'))
        d = datetime.timedelta(minutes=m, seconds=s)
        if d < timeLeft: 
            print("You can fit " + songName + " into your playlist.")
    print("time left: " + str(timeLeft))

Demo