You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中实现无需输入的10秒自动倒计时器?

修改方案

直接调整代码的输入触发逻辑,去掉手动输入秒数的步骤,改为等待用户按下回车键后启动固定10秒的倒计时,具体修改如下:

关键修改点

  • 移除原来的秒数输入语句,替换为等待用户按回车的触发逻辑
  • 直接传入固定值10给countdown函数,无需用户输入时间

修改后的完整代码

import time
import datetime
 
def countdown(s):
    total_seconds = s
    while total_seconds > 0:
        timer = datetime.timedelta(seconds=total_seconds)
        print(timer, end="\r")
        time.sleep(1)
        total_seconds -= 1
    print("You have been caught.")
 
# 等待用户按下回车键启动倒计时
input("Press Enter to start 10-second countdown...")
countdown(10)

说明

  • 运行代码后,会提示用户按回车,用户按下回车后立刻开始10秒倒计时
  • 倒计时过程中会实时刷新显示剩余时间,结束后输出指定提示语

内容的提问来源于stack exchange,提问作者Louise Mary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 04:01:07