如何使用 a_timedelta.total_seconds() 创建一个在低于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论