2017-09-27 91 views
0

所以我一直在玩一点点的时间表,我发现Schedule github有时间表,也非常好,易于使用。所以我迄今所做的是:Python - 格式/修剪,以便它给出正确的时间?

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秒。所以我想知道是否有一种方法可以修剪/剪切/格式化它,以便它在5秒钟后开始打印?

编辑OUTPUTT:

-------------------------------------- 
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 

回答

1

因此,修剪浮动简单的方法是:int(variable)但你可以做,没有装饰。试试这个: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') 
+0

我认为,即时通讯越来越奇怪错误。然而,当我尝试运行这个,它确实说程序开始于:....但后来告诉我,我的输入是错误的..那多奇怪..我编辑我的线程,让你知道输出 – WeInThis

+0

嗯...现在它的股价下跌至-0 ... ''' 计划开始于...: 2.733921 计划在开始...: 1.733449 计划开始于...: 0.732948 计划的开始...: -0.267551 -------------------------------------- 错误的输入 - 再试一次 -------------------------------------- 要运行计划任务 - 按y 要直接运行 - 按n ''' – WeInThis

+0

我认为不是有'break'我可以使用main()。对? – WeInThis

1

该函数返回一个浮点数,所以它比较字符串不会有任何效果。 然而,你也许可以做线沿线的东西:

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

如果你想成为安全起见,也可以落地的,而不是圆形:

idle = int(schedule.idle_seconds()) 
+0

这没有奏效。所以当我尝试改变为''' idle = int(round(schedule。idle_seconds())) 而真: 打印(空闲) schedule.run_pending() time.sleep(1) idletwo = INT(schedule.idle_seconds()) 如果(空闲== '5'): print(“Test:”+ str(idle)+'\ n') ''' 它刚刚被卡住在同一秒钟。所以如果还剩36秒。它只是打印出36 36 36 36 36 – WeInThis

+0

'如果空闲== 5'不等于'如果空闲=='5'',但是当你的问题解决了,无论如何,我想我们不需要进入这个-深度。 :) – Uvar

+0

我不这么认为!谢谢你。没有你的“回合”我不会得到它! – WeInThis