Search code examples
python-3.xtimershutdown

Timer shutdown computer(python3.x)


i had code a timer shutdown pc which will shutdown after the times up, but it will keep printing the time remaining which is not good if i want to shutdown my computer after 30 minutes, it will print about 1800 lines, how should i modify it if i want it to print one line of time remaining which will keep changing.

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

Solution

  • Try this. Just replace the string "Shutdown has started" with the shutdown command.

    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)