如何使用 a_timedelta.total_seconds() 创建一个在低于 0 时中断的倒计时器?

发布于 2025-01-14 18:57:17 字数 1108 浏览 0 评论 0原文

import time,datetime
end_game = False
count=0
timer=10
date_and_time1 = datetime.datetime.now()
while True:
        if end_game == True:
          break
        choice3 = input("Break the clock.")
        
        if count == 4:
              print("YOU ARE OUT OF DEBT!")
              true = False
              break
        else:
              if choice3 == "break":
                date_and_time2 = datetime.datetime.now()
                a_timedelta = date_and_time2 - date_and_time1
                seconds = a_timedelta.total_seconds()
                if seconds <= 0:
                  print("You did not break the clock fast enough...")
                  time.sleep(2)
                  print("You lose.")
                  end_game = True
                elif seconds > 0:
                  count += 1
                  print(timer - seconds, " seconds left")
              else:
                print("The timer is running out. ('break')")

我对 python 很陌生。我正在尝试创建一个定时迷你游戏,作为较大程序的一部分,用户只需在 10 秒内输入“break”5 次即可获胜。目前,当计时器超时(秒<0)时,循环不会按其应有的方式中断。有没有简单的方法来解决这个问题?

import time,datetime
end_game = False
count=0
timer=10
date_and_time1 = datetime.datetime.now()
while True:
        if end_game == True:
          break
        choice3 = input("Break the clock.")
        
        if count == 4:
              print("YOU ARE OUT OF DEBT!")
              true = False
              break
        else:
              if choice3 == "break":
                date_and_time2 = datetime.datetime.now()
                a_timedelta = date_and_time2 - date_and_time1
                seconds = a_timedelta.total_seconds()
                if seconds <= 0:
                  print("You did not break the clock fast enough...")
                  time.sleep(2)
                  print("You lose.")
                  end_game = True
                elif seconds > 0:
                  count += 1
                  print(timer - seconds, " seconds left")
              else:
                print("The timer is running out. ('break')")

I am quite new to python. I am trying to create a timed mini game as part of a larger program where the user simply has to type 'break' 5x within 10 seconds in order to win. At the moment, when the timer runs out (seconds < 0), the loop does not break as it should. Is there a simple way to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文