title: 24 game using Python
body: <p>There is a game called 24, which is a math-based game. If you are new to the game, please read this link
  <a href="http://en.wikipedia.org/wiki/24_Game" rel="nofollow">http://en.wikipedia.org/wiki/24_Game</a></p>
  
  <p>Anyways, this is a game where you get four numbers (in my cases, from 1 to 9) that are randomly selected, and try to combine them (using the four basic operations +, -, •, and ÷) to make the number 24. The rules are simple, you must use all four numbers and only those four numbers, and they can only be used once each. This game intrigued me enough to try and make a program to simulate this game.</p>
  
  <p>My goal is to create a program like this using python. The basic algorithm is...</p>
  
  <ul>
  <li>Generate 4 random numbers</li>
  <li>check if they are equal to 24</li>
  <li>display them to the user</li>
  <li>see if the user's input also is equal to 24</li>
  </ul>
  
  <p><strong><em>I need the most help with step 2 and 4</em></strong></p>
  
  <p>I will use the random module to decide random integers</p>
  
  <pre><code>import random
  num1 = random.randint ( 1, 9)
  num2 = random.randint ( 1, 9)
  num3 = random.randint ( 1, 9)
  num4 = random.randint ( 1, 9)
  </code></pre>
  
  <p>So, please, if you can, help me create this game, and explain it to me too! thanks. </p>
  
  <p>Also, this is my first time using stack overflow to ask a question, so please comment on how i can improve my asking skills :)</p>
  
  <p><strong><em>---EDIT---</em></strong>
  Thanks for your help guys, but i finally figured it out. I did a bit of research and compiled together this (if any of you wish to try the code for yourselves):</p>
  
  <p>></p>
  
  <pre><code> '''
   The 24 Game
  
   Given any four digits in the range 1 to 9, which may have repetitions,
   Using just the +, -, *, and / operators; and the possible use of
   brackets, (), show how to make an answer of 24.
  
   An answer of "!" will generate a new set of four digits (if your stuck).
   Otherwise you are repeatedly asked for an expression until it evaluates to 24
  
   Note: you cannot form multiple digit numbers from the supplied digits,
   so an answer of 12+12 when given 1, 2, 2, and 1 would not be allowed.
  
  '''
  from __future__ import division, print_function
  import random, ast, re
  import sys
  
  while 1 == 1: 
  
  
  if sys.version_info[0] &lt; 3: input = raw_input
  
  def choose4():
      'four random digits &gt;0 as characters'
      return [str(random.randint(1,9)) for i in range(4)]
  
  def welcome(digits):
      print (__doc__)
      print ("Your four digits: " + ' '.join(digits))
  
  def check(answer, digits):
      allowed = set('() +-*/\t'+''.join(digits))
      ok = all(ch in allowed for ch in answer) and \
           all(digits.count(dig) == answer.count(dig) for dig in set(digits)) \
           and not re.search('\d\d', answer)
      if ok:
          try:
              ast.parse(answer)
          except:
              ok = False
      return ok
  
  def main():    
      digits = choose4()
      welcome(digits)
      trial = 0
      answer = ''
      chk = ans = False
      while not (chk and ans == 24):
          trial +=1
          answer = input("Expression %i: " % trial)
          chk = check(answer, digits)
          if answer.lower() == 'q':
              break
          if answer == '!':
              digits = choose4()
              print ("New digits:", ' '.join(digits))
              continue
          if not chk:
              print ("The input '%s' was wonky!" % answer)
          else:
              ans = eval(answer)
              print (" = ", ans)
              if ans == 24:
                  print ("Thats right!")   
  main()
  </code></pre>
  
  <p>(python 3.3)</p>