Python 写一个倒计时程序

# 模拟特价商品销售倒计时提示
import datetime     # 引入datetime模块
import time         # 引入time模块

AScheduledTime = datetime.datetime(2023, 1, 21, 0, 0, 0)  # 设置预定时间
Anow = datetime.datetime.today()                          # 获取现在的时间
ADifference = AScheduledTime - Anow                       # 存储两个时间的时间,差精确到毫秒
Asec = ADifference.seconds                                # 距离预定时间还有多少秒

while (Asec):
    while (1):
        ScheduledTime = datetime.datetime(2023, 1, 21, 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

运行结果:

 

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值