Python 倒数计时器问题

Python Countdown Timer Issue

我无法让这个倒数计时器正常工作。 while 循环中的 2 个 print 语句打印在同一行上,大多数时候速度太快,看不到。 P.S。我是 Python 的新手,所以如果我不理解所有内容,请原谅。谢谢!

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

您在两个打印语句中都使用了 \r,有效地让一个覆盖了另一个。此外,在 while 循环中,您希望不断调整时间。这是我做的一个小调整。也许您现在可以自己在其中添加您的第二个声明。

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