【问题标题】:display how much time is left until shutdown显示距离关机还有多长时间
【发布时间】:2020-01-14 13:35:39
【问题描述】:
import time as t
import os

print("Timed Shutdown")
run = int(input("Please enter a shutdown time in minutes: "))
run *= 60
while run != 0:
    run -= 1
    t.sleep(1)
    print("Your system willl close in:", str(int(run/60)), "minutes")

os.system("shutdown -s")

我正在尝试制作一个程序,在用户输入的特定时间(以分钟为单位)后关闭计算机。我想从 while 循环内部制作打印语句,以便用户仅在经过一分钟后(或 t 时间之后)才能看到。

【问题讨论】:

  • 您想在循环结束后立即打印整个倒计时?计算机的关闭不会阻止他们阅读此内容吗?
  • 我想在 T 间隔打印一条消息,告诉用户在计算机关闭之前他还有多少时间。请注意,我这样做是出于学习目的:)
  • 因此,例如,它每 30 秒打印一次还剩多少分钟?
  • 是的,类似的

标签: python-3.x time os.system


【解决方案1】:
import time as t
import os

interval = 30
print("Timed Shutdown")
run = int(input("Please enter a shutdown time in minutes: "))
run *= 60
while run != 0:
    run -= 1
    t.sleep(1)
    if run % interval == 0:
        print("Your system will close in:", str(int(run/60)), "minutes")

os.system("shutdown -s")

如果剩余时间是interval 的倍数,则将打印剩余时间。

【讨论】:

    【解决方案2】:

    添加一个display_time 变量,然后检查run % display_time == 0

    import time as t
    import os
    
    print("Timed Shutdown")
    run = int(input("Please enter a shutdown time in minutes: "))
    run *= 60
    display_time = 60 # number of seconds between displays
    while run != 0:
        run -= 1
        t.sleep(1)
        if run % display_time == 0:
            print("Your system willl close in:", str(int(run/60)), "minutes")
    print("hi")
    # os.system("shutdown -s")
    

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 2018-03-31
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多