弄了一个倒计时程序(支持一直倒计时至0),但是不完美。期待与广大的高手交流。
废话不多说,直接上代码。
# coding :UTF_8
# 开发团队 :心心相印
# 开发人员 :机器人小白
# 开发时间 :2020/5/30 16:16
# 文件名称 :AC_3_4.py
# 开发工具 :PyCharm
# 模拟特价商品销售倒计时提示
import datetime # 引入datetime模块
import time # 引入time模块
AScheduledTime = datetime.datetime(2020, 6, 18, 0, 0, 0) # 设置预定时间
Anow = datetime.datetime.today() # 获取现在的时间
ADifference = AScheduledTime - Anow # 存储两个时间的时间,差精确到毫秒
Asec = ADifference.seconds # 距离预定时间还有多少秒
while (Asec):
while (1):
ScheduledTime = datetime.datetime(2020, 6, 18, 0, 0, 0) # 设置预定时间
now = datetime.datetime.today() # 获取现在的时间
Difference = ScheduledTime - now # 存储两个时间的时间,差精确到毫秒
day = Difference.days # 距离预定时间还有多少天
sec = Difference.seconds # 距离预定时间还有多少秒
hour = int(sec / 60 / 60) # 距离预定时间还有多少小时,并换算为int型
minute = int(sec / 60 % 60) # 距离预定时间还有多少分钟,并换算为int型
second = sec % 60 # 距离预定时间还有多少秒,并换算为int型
print('距离结束:' + str(day) + '天 ' + str(hour) + '时 ' + str(minute) + '分 ' + str(second) + '秒')
time.sleep(1)
Asec -= 1
运行结果: