0

I'm pretty new to programming and I'm trying to create a simple card game. In the game, the player has to be able end his turn by typing 'next'. This is followed by the program sleeping for a few seconds. However, if the player types 'next' during the sleep time, the player's next turn will be skipped.

My (simplified) code:

import time


def foo():
    a = input()
    if str(a) == 'next':
        print('next')
        return
    else:
        foo()
        return

foo()
time.sleep(5)
foo()

If you type 'next' within the five seconds of sleep, the last foo() will automatically print 'next'. How can I make the program ignore commands typed during the sleep?

Raijine
  • 1
  • 1
  • 1
    I think you misunderstand what's happening here. `foo` doesn't end and `time.sleep` isn't reached until **after** the user has entered their input. If you don't enter anything, you will stay on the line `a = input()` indefinitely. – jonrsharpe Jul 05 '15 at 10:13
  • @jonrsharpe I believe he was talking about stdin being buffered when time.sleep() is occuring , so even if the program is sleeping, if you type anything in , it will get buffered into stdin I believe. – Anand S Kumar Jul 05 '15 at 10:14
  • On a side note, you should be using a while loop, your second return is not doing anything and a is already a string – Padraic Cunningham Jul 05 '15 at 10:16
  • @AnandSKumar Yep, that's what I mean. Is there a way to remove that from stdin? – Raijine Jul 05 '15 at 10:17
  • @PadraicCunningham Thanks, I'll change that – Raijine Jul 05 '15 at 10:18

0 Answers0