0

Basically, I want my throw raw input variable to be skipped so that it can still be accessed and referred to later on, but doesn't need a user input to continue, so what happens is if you type "throw brick" within 5 seconds, it will bring you to the outcome print "good job!"

throw = raw_input()
    throw_command = ["throw brick"]
    import time
    me.sleep(5)
    if throw in throw_command:
        print "good job!"
    if throw not in throw_command:
        print "GAME OVER"
        restart

I know such thing as a goto or jump don't exist in such a structure-based code. So, if anyone could happen to provide an alternative, your assistance would be much appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • To avoid synchronous input processing in a console application look into a library like [curses](https://docs.python.org/3/library/curses.html) (or equivalent, but hopefully at a higher level). – user2864740 Aug 30 '14 at 23:43
  • Your code isn't syntactically correct, so it doesn't do a good job of illustrating what your question is. Try creating a complete but minimal sample program that that tries to implement what you want to do and edit your post to included it. Describe what it should be doing, and what it's actually doing. Cut and paste any output or error messages in generates and include that in your question. – Ross Ridge Sep 01 '14 at 00:27

1 Answers1

0

I'm not sure I understand what you're trying to do, but it sounds to me like this:

  • Ask the user for input, but timeout after 5 seconds (your time.sleep(5))
  • Repeat over and over again (your restart)

If so, it looks like this would be one way to do it: Keyboard input with timeout in Python

I modified the select-based answer with the details of your code:

import sys
import select

throw_command = ["throw brick"]

while True:
    print "\nEnter command:"
    i, o, e = select.select( [sys.stdin], [], [], 5 ) # times out after 5 seconds

    if i: # If input was provided
        throw = sys.stdin.readline().strip()
        if throw in throw_command:
            print "good job!"
            break
        else:
            print "GAME OVER"
    else:
      print "You didn't enter anything!"

The use of select is a little advanced (ideally, raw_input should have an optional timeout argument) but it's easier than using threads which is the only way I could think of doing this with raw_input.

The use of a while loop instead of your restart attempt at a go-to should be self-explanatory.

Community
  • 1
  • 1
Cameron Lee
  • 973
  • 9
  • 11