【问题标题】:Timer shutdown computer(python3.x)定时器关机电脑(python3.x)
【发布时间】:2021-05-13 07:42:56
【问题描述】:
我编写了一个定时器关机电脑,它会在超时后关机,但它会继续打印剩余时间,如果我想在 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")
【问题讨论】:
标签:
python-3.x
timer
shutdown
【解决方案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)