如果你等了4秒就会说“你没时间了”这很好.但是,为了保持循环,你必须按回车键继续.
我想这样当它打印下面的“你没时间”而不是只是输入时,它会显示一个输入语句,如“类型’攻击’继续”,循环将从它的位置继续.
from threading import Timer
import time
monsterhp = int(800)
y = 150
while monsterhp > 0:
timeout = 4
t = Timer(timeout, print, ['You ran out of time.'])
t.start()
print(" ")
prompt = "You have %d seconds Type 'attack' to hit the monster\nType here: " % timeout
answer = input(prompt)
t.cancel()
if answer == "attack":
print("You strike the monster")
time.sleep(1)
monsterhp = monsterhp - y
print("War Lord Health:", monsterhp)
最佳答案 完成你提出的任务并不像你猜想的那么容易.使用信号模块更容易:(我已将代码与修改后的版本I
linked合并)
import signal, time
def TimedInput(prompt='', timeout=20,