【问题标题】:How to set a timeout for Input如何设置输入超时
【发布时间】:2017-05-18 01:12:24
【问题描述】:

如果您等待 4 秒,它会显示“您的时间已用完”,这很好。但是,要继续循环,您必须按enter 键才能继续。

我希望当它在下面打印“你没有时间”而不是仅仅输入时,它会显示一个输入语句,例如“输入'攻击'以继续进行”,并且循环将从原来的位置继续。

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)

【问题讨论】:

  • @abccd 我不完全确定我的意思是从他的回复中得到什么 我对线程和线程仍然很陌生,你能告诉我需要去哪里吗?谢谢,它必须有一个功能还是我们仍然可以使用我的大部分版本?
  • 您使用的是哪个操作系统?
  • Mac OS 最新python

标签: python python-3.x


【解决方案1】:

有一个新库 inputimeout 用于标准输入超时

$ pip install inputimeout

用法

from inputimeout import inputimeout, TimeoutOccurred
try:
    string = inputimeout(prompt='>>', timeout=5)
except TimeoutOccurred:
    string = 'time is over'
print(string)

【讨论】:

  • 这不起作用。这总是超时并且不检查在超时之前输入的值。
  • @AbhishekRai 你做错了什么,它工作得很好,尝试设置更长的超时时间,写完输入后记得按回车。分享您的代码
  • 让我再试一次。不..失败。这就是有效的..在Windows上。 stackoverflow.com/questions/66262161/…
  • @AbhishekRai 我在 Windows 10 上,inputimeout 就像一个魅力,看图片i.imgur.com/6iEH9T6.png
【解决方案2】:

执行您提议的任务并不像您想象的那么容易。使用 signal 模块更容易做到这一点:(我已将您的代码与答案 I linked 的修改版本合并)

import signal, time

def TimedInput(prompt='', timeout=20, timeoutmsg = None):
    def timeout_error(*_):
        raise TimeoutError
    signal.signal(signal.SIGALRM, timeout_error)
    signal.alarm(timeout)
    try:
        answer = input(prompt)
        signal.alarm(0)
        return answer
    except TimeoutError:   
        if timeoutmsg:
            print(timeoutmsg)
        signal.signal(signal.SIGALRM, signal.SIG_IGN)
        return None

monsterhp = int(800)
y = 150
while monsterhp > 0:
    timeout = 4
    timeoutmsg = 'You ran out of time.'
    print(" ")
    prompt = "You have %d seconds Type 'attack' to hit the monster\nType here: " % timeout
    answer = TimedInput(prompt, timeout, timeoutmsg)

    if answer == "attack":
        print("You strike the monster")
        time.sleep(1)
        monsterhp = monsterhp - y
        print("War Lord Health:", monsterhp)

注意:这仅适用于所有 unix/mac 系统

您可以将您的 while 循环更改为此,以获得代码的改进版本:)

while monsterhp > 0:
        timeout = 4
        timeoutmsg = 'You ran out of time.'
        print(" ")
        prompt = "You have %d seconds Type 'attack' to hit the monster\nType here: " % timeout
        answer = TimedInput(prompt, timeout, timeoutmsg)

        if answer == "attack":
            print("You strike the monster")
            time.sleep(1)
            monsterhp = monsterhp - y
            print("War Lord Health:", monsterhp)
        elif answer == None:
            print("The War Lord has killed you, you're now dead")
            print("Thanks for playing, \nGAME OVER")
            break

【讨论】:

  • 哇,我完全没想到。顺便说一句,我简化了我的代码版本,以便更容易理解。 pastebin.com/mbYgyucJ 。这接近你的第二个答案啊哈。但是我将不得不在另一个小时内测试 TimedInput,因为 atm 很忙。还是谢谢你的回复。
  • 哇,谢谢它的工作,我不敢相信有这么多代码。再次感谢
猜你喜欢
  • 1970-01-01
  • 2022-12-05
  • 1970-01-01
  • 2014-07-23
  • 1970-01-01
  • 2014-08-22
  • 2021-12-04
  • 1970-01-01
  • 2019-06-25
相关资源
最近更新 更多