title: python integrating code with raw_input timeout
body: <p>This is most of my code for my now working python blackjack game (or at least blackjack like game) I have now been told that I need to implement a time limit (user gets asked to input something and only has 3 or so seconds to give a response).         </p>
  
  <pre><code>def deck():
          cards = range(1, 12)
          return choice(cards)
  
  
  def dealer():
      total = deck() + deck()
      if total &lt;= 15:
          totalf = total + deck()
          while totalf &lt;= 21:
              return totalf
      if total &gt; 15:
          return total
  
  def player():
      card1 = deck()
      card2 = deck()
      hand = card1 + card2
      print "Cards dealt: %d and %d" % (card1, card2)
      while hand &lt;= 21:
          choice = raw_input("Would you like to hit or stand?: ")
          print choice
          if choice == "hit":
              hand = hand + deck()
              print "Current Total: %d" % hand        
          elif choice == "stand": 
              return hand 
  
  money = 100
  highscore = 0
  
  while money &gt; 0:
      opp = dealer()
      me = player()
      if me &gt; opp:
          highscore = highscore + 10
          money = money + 10
          print "Winner, winner, chicken dinner! You have $%d!" % money
          print "********************************************"
      elif opp &gt; 21:
          highscore = highscore + 10
          money = money + 10
          print "Winner, winner, chicken dinner! You have $%d!" % money
          print "********************************************"
      elif me &gt; 21:
          money = money - 20
          print "Bust! Dealer wins with %d. You have $%d reamaining." % (opp, money)      
          print "********************************************"
      elif opp &gt; me:
          money = money - 20
          print "Dealer wins with %d. You have $%d reamaining." % (opp, money)
          print "********************************************"
      elif me == 21:
          highscore = highscore + 10
          money = money + 10
          print "Blackjack! You have $%d!" % money
          print "********************************************"
      sleep(1)    
  
  print "Thank you for playing! Your highscore was $%d." % highscore
  </code></pre>
  
  <p>This is the code my professor has provided us with to do this: </p>
  
  <pre><code>   import sys, time
  from select import select
  
  import platform
  if platform.system() == "Windows":
      import msvcrt
  
  def input_with_timeout_sane(prompt, timeout, default):
      """Read an input from the user or timeout"""
      print prompt,
      sys.stdout.flush()
      rlist, _, _ = select([sys.stdin], [], [], timeout)
      if rlist:
          s = sys.stdin.readline().replace('\n','')
      else:
          s = default
          print s
      return s
  
  def input_with_timeout_windows(prompt, timeout, default): 
      start_time = time.time()
      print prompt,
      sys.stdout.flush()
      input = ''
      while True:
          if msvcrt.kbhit():
              chr = msvcrt.getche()
              if ord(chr) == 13: # enter_key
                  break
              elif ord(chr) &gt;= 32: #space_char
                  input += chr
          if len(input) == 0 and (time.time() - start_time) &gt; timeout:
              break
      if len(input) &gt; 0:
          return input
      else:
          return default
  
  def input_with_timeout(prompt, timeout, default=''):
      if platform.system() == "Windows":
          return input_with_timeout_windows(prompt, timeout, default)
      else:
          return input_with_timeout_sane(prompt, timeout, default)
  </code></pre>
  
  <p>I am completely lost how to merge these two pieces of code. I've tried for the past couple hours to get it to work but for whatever reason its just not working. Any help would be amazing. (I apologize for the wall of code).</p>