输入时间限制

Input Time Limit

我正在制作 Python 我们之间的游戏(有点)。我需要一些帮助来限制人们输入文本的速度。看看我下面的代码。

import random
print("Python Among Us")
print()
role = ["Crewmate", "Crewmate" , "Crewmate", "Crewmate", "Crewmate", "Crewmate", "Crewmate", "Crewmate", "Crewmate", "Impostor"]
roleconfirmed = random.choice(role)
print("You are a", roleconfirmed, "!")
if roleconfirmed == ("Crewmate"):
    firstdestination = input("Where would you like to go? ")
    print("You are currently in", firstdestination)
    if firstdestination == ("Admin"):
        print("You have no tasks in this section.")
    elif firstdestination == ("Cafeteria"):
        print("You have one task, that task is Empty Garbage.")
        wouldliketodotask = input("Would you like to do your tasks? ")
        if wouldliketodotask == ("Yes"):
            whichtasktodo = input("Which task would you like to do? ")
            if whichtasktodo == ("Empty Garbage"):
                emptygarbagetask = input("You have 5 seconds to type Swipe Down: ")
                if emptygarbagetask :
                    print("You failed to do the task!")
                else: 
                    print("You completed the task!")

在最下面,我把if emptygarbagetask :留了下来,希望大家帮我解决。在你说“哦,看看其他问题”之前,我的回答是,是的,我已经看过了,但我很难把它放在那里。

import time
from threading import Thread

answer = None

def check():
    time.sleep(2)
    if answer != None:
        return
    print("Too Slow")

Thread(target = check).start()

answer = input("Input something: ")

你可以看到这个question以获得更多答案

import time 在开头,然后:

...
if whichtasktodo == "Empty Garbage":
    start_time = time.time()
    ...
    end_time = time.time()
    if (emptygarbagetask = "Swipe Down") and (end_time - start_time <= 5):
        ...
...

pip install inputimeout

from inputimeout import inputimeout, TimeoutOccurred

if __name__ == "__main__":
    try:
        c = inputimeout(prompt='hello\n', timeout=3)
    except TimeoutOccurred:
        c = 'timeout'
    print(c)

我认为您可以轻松地将此答案集成到您的代码中。