Python - Format/Trim 所以它给出了正确的时间?

Python - Format/Trim so it gives the right time?

所以我一直在研究日程安排,我发现 Schedule github 有日程安排,而且非常好用且易于使用。所以到目前为止我所做的是:

UserInput = input('To run Schedule script - Press y\nTo run directly - Press n\n')

if(UserInput == 'n'):
    main()
elif(UserInput == 'y'):
    TimeUser = input('What time to start script? Format - HH:MM ')
    schedule.every().day.at(TimeUser).do(main)
    wipe()
    print('Schedule starts at: ' + TimeUser + ' - Waiting for time...')

    while True:
         schedule.run_pending()
         time.sleep(1)
         if(schedule.idle_seconds() == '5'):
                 print('Program starts in...:\n' + str(schedule.idle_seconds()) + '\n')

然而我现在遇到的问题是我的输出结果是

程序开始于...: 30.08442

Program starts in...:
29.083967

Program starts in...:
28.083956

Program starts in...:
27.083923

基本上我想做的是 if(schedule.idle_seconds()): 5 秒。所以当它还剩 5 秒时,它应该开始打印出来。但是,我遇到的问题是,由于我认为是毫秒,它永远不会达到 5 秒。所以我想知道是否有办法 trim/cut/format 它会在还剩 5 秒时开始打印出来?

编辑输出:

--------------------------------------
Schedule starts at: 13:55 - Waiting for time...
--------------------------------------
Program starts in...:
4.748427

--------------------------------------
Wrong input - Try again
--------------------------------------
To run Schedule task - Press y
To run directly - Press n

因此,trim 浮动的最简单方法是:int(variable) 但您可以在没有 trim 的情况下做到这一点。试试这个:if(schedule.idle_seconds() < 5.0): 而不是你的条件。但它将永无止境。如果你想停止循环工作,你必须创建额外的条件。

例如:

while True:
    schedule.run_pending()
    time.sleep(1)
    idle = schedule.idle_seconds()
    if(idle < 5.0) and (idle >= 0.0):
        print('Program starts in...:\n' + str(schedule.idle_seconds()) + '\n')

函数 returns 是一个浮点数,因此将它与字符串进行比较是行不通的。 但是,您可以按照以下方式做一些事情:

idle = int(round(schedule.idle_seconds()))
if idle == 5:
    print('Program starts in...:\n' + str(idle) + '\n')

如果为了保险起见,也可以地板代替圆形:

idle = int(schedule.idle_seconds())