简体   繁体   中英

Python Countdown Timer Issue

I'm having trouble getting this countdown timer to work. The 2 print statements within the while loop print on the same line and it's too fast to see most of the time. PS I'm kind of new to Python so excuse me if I don't understand everything. Thanks!

import time
import datetime
eh = datetime.datetime(2019,3,31,20,30)
now = datetime.datetime.now()
print("Earth Hour!: " + eh.strftime("%d-%m-%Y %H:%M:%S"))
tte = eh - now
while eh > now:
    now = datetime.datetime.now()
    print("Current Time: " + now.strftime("%d-%m-%Y %H:%M:%S"), end="\r")
    print("Time Till Earth Hour: " + str(tte), end = "\r")

You are using a \\r for both print statements, effectively letting the one override the other. Furthermore in the while loop you want to continuously adjust the time. Here is a small adjustment i made. Perhaps you can add your second statement in there yourself now.

import time
import datetime
eh = datetime.datetime(2019,3,31,20,30)
print("Earth Hour!: " + eh.strftime("%d-%m-%Y %H:%M:%S"))
now = datetime.datetime.now()


while eh > now:
    now = datetime.datetime.now()
    tte = eh - now
    print("Time Till Earth Hour: " + str(tte), end = "\r")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM