0

I'm making a Python Among Us game (kinda). I need some help on making a time limit on how quick the person has to input the text. Look at my code below.

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!")

At the bottom, I left the if emptygarbagetask : open, for the community to help me solve. Before you say "oh look at the other questions" my answer is, yes I have looked, but I am having trouble trying to put it in there.

Andrew Liu
  • 78
  • 8

3 Answers3

1

import time at the start of it, then:

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

pip install inputimeout

and

from inputimeout import inputimeout, TimeoutOccurred

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

I think you will be able to integrate this answer into your code easily.

Sridhar Raju
  • 430
  • 1
  • 10
0
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: ")

you can see this question for more answers

Sridhar Raju
  • 430
  • 1
  • 10