0

I'm making a simple Python Text Adventure Game, and I want to know a way how to make a simple function that asks for a user input; however, I want the program to ONLY accept the input before a certain duration (in my program I'm using 3 seconds). Furthermore, I want the function to check the correctness of the input before accepting it as right. My attempt is as follows:

lvl1 = [
 'Hi', 'Friend', 'Say', 'Smelly',
 'Made', 'Tree', 'Python', 'Freak'
]

one = raw_input("You have 3 seconds to type: " + random.choice(lvl1))
endtime = time.time() + 3
while time.time() <= endtime and lvl1[0:]: 
  print("Wrong!")
  one = raw_input("Try again: ")
print("Correct")

Basically what I want this code to do is to get an input from the user in (less than or equal to) a 3 second time duration, but before congratulating the user, make sure that their input is the same as one of the strings in the 'lvl1' array.

Thanks!

  • 7
    Duplicate of http://stackoverflow.com/questions/2933399/how-to-set-time-limit-on-input, http://stackoverflow.com/questions/14029548/input-with-time-limit-countdown, http://stackoverflow.com/questions/15528939/python-3-timed-input, http://stackoverflow.com/questions/12667707/raw-input-with-a-time-limit – alksdjg Sep 30 '15 at 21:58

1 Answers1

0

You were very close. One thing that you missed was that you need to start the timer before you prompt the user for input. If you do not do this, the program will wait for the users input before starting the timer, which will always cause the "in less than 3 seconds" condition to be true. Another change I made was storing the value of the randomly selected word so that you can display it and, later in the program, verify that the user's input matches later.

import random
import time

lvl1 = [
 'Hi', 'Friend', 'Say', 'Smelly',
 'Made', 'Tree', 'Python', 'Freak'
]

word = random.choice(lvl1)
start_time = time.time()
one = raw_input("You have 3 seconds to type: {}\n".format(word))

while time.time() - start_time > 3 or one != word:
    print("Wrong!")
    start_time = time.time()
    one = raw_input("Try again: ")
print("Correct")

Hope this helps!

pzp
  • 6,249
  • 1
  • 26
  • 38
  • `raw_input` and `input` block, so this'll never work. I think the question was badly phrased: usually, in this type of game, after the time is up, the game continues so this strategy can't be used. –  Sep 30 '15 at 22:06
  • @gecko And that's precisely why I set ``start_time`` before I call ``raw_input()``. Just try running it; it works. – pzp Sep 30 '15 at 22:07
  • See edited comment. It's not about waiting for input then failing if it took too long. It's about removing the option to answer after the timer elapses. i.e. interactive/realtime behaviour. –  Sep 30 '15 at 22:09
  • @gecko Ah, well it appears one of us misunderstood the OP. Based on the OP's code I thought that he/she just wanted to reject the answer if the user took more than 3 seconds. But you might be right that OP wants it to reject as soon as the 3 seconds are up (not when the user submits after 3 seconds). – pzp Sep 30 '15 at 22:13