定时关机(python3.x)

Timer shutdown computer(python3.x)

我编写了一个定时器关机电脑,它会在时间到后关机,但它会继续打印剩余时间,如果我想在 30 分钟后关机,这不太好,它会打印大约 1800 行,如果我想让它打印一行剩余时间,它会不断变化,我应该如何修改它。

import time
seconds = int(input("seconds:"))
for i in range(seconds):
    x = (seconds - i)
    print(x)
    time.sleep(1)

check = input("do u want to shutdown ur computer?(yes/no):")

if check == "no":
    exit()
else:
    os.system("shutdown /s /t 1")

试试这个。只需将字符串 "Shutdown has started" 替换为关机命令即可。

import time
import sys
x=int(input("seconds: "))
print("The timer has started. Time remaining for shut down: ")
def custom_print(string, how = "normal", dur = 0, inline = True):
    if how == "typing": # if how is equal to typing then run this block of code
        letter = 1
        while letter <= len(string):
            new_string = string[0:letter]
            if inline: sys.stdout.write("\r")
            sys.stdout.write("{0}".format(new_string))
            if inline == False: sys.stdout.write("\n")
            if inline: sys.stdout.flush()
            letter += 1 
            time.sleep(float(dur))
            if new_string=="0": 
                print("\nShut down has started")
            else: 
                pass
for k in range(1,x+1):
    k=x-k
    custom_print(str(k), "typing", 1)