【问题标题】:Python - Format/Trim so it gives the right time?Python - 格式化/修剪以便提供正确的时间?
【发布时间】:2018-03-08 20:54:31
【问题描述】:

所以我一直在玩一些日程安排,我发现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 秒,因为我认为是毫秒。所以我想知道是否有办法修剪/剪切/格式化它,这样它会在剩下 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

【问题讨论】:

    标签: python time format scheduled-tasks trim


    【解决方案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... ``` 程序开始于...:2.733921 程序开始于...:1.733449 程序开始于...:0.732948 程序开始于...:-0.267551 -------------------------- 输入错误 - 尝试再次 -------------------------- 运行计划任务 - 按 y 直接运行 -按 n ```
    • 我认为不用break 我可以只使用main()。对吗?
    • 可能是函数中break引起的。我修复了错误,试试这个版本,
    • 耶,成功了!我还做了idle = int(round(schedule.idle_seconds())) 对其进行四舍五入,所以它给了我一个完整的数字而不是 4.412412
    【解决方案2】:

    该函数返回一个浮点数,因此将它与字符串进行比较永远不会起作用。 但是,您可能可以按照以下方式做一些事情:

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

    如果你想安全起见,你也可以用地板代替圆形:

    idle = int(schedule.idle_seconds())
    

    【讨论】:

    • 那行不通。所以很明显,当我尝试更改为 ``` idle = int(round(schedule.idle_seconds())) while True: print(idle) schedule.run_pending() time.sleep(1) idletwo = int(schedule.idle_seconds( )) if(idle == '5'): print("Test: " + str(idle) + '\n') ``` 它只是卡在同一秒。因此,如果还剩 36 秒。它刚刚打印出 36 36 36 36 36
    • if idle == 5 不等于if idle == '5',但是无论如何你的问题已经解决了,我想我们不需要深入探讨这个问题。 :)
    • 我不这么认为!也谢谢你。没有你的“圆”,我也不会得到它!
    猜你喜欢
    • 2020-11-22
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-25
    相关资源
    最近更新 更多