title: How to consolidate if / elif statements in Python
body: <p>I looked around and did some research, but for some reason I still couldn't get it to work as planned. Basically, as a beginner, I wrote a Mastermind code game- same rules and all. Here's the code:</p>
  
  <pre><code>import random
  trial = 0
  def geuss():
      geuss = raw_input("What is your geuss? ")
      global g1
      global g2
      global g3
      global g4
      a = geuss[:-3]
      b = geuss[1:-2]
      c = geuss[2:-1]
      d = geuss[3:]
      if a == peg1:
          g1 = 'R'
      elif a == peg2:
          g1 = 'W'
      elif a == peg3:
          g1 = 'W'
      elif a == peg4:
          g1 = 'W'
      else:
          g1 = 'X'
  
      if b == peg2:
          g2 = 'R'
      elif b == peg1:
          g2 = 'W'
      elif b == peg3:
          g2 = 'W'
      elif b == peg4:
          g2 = 'W'
      else:
          g2 = 'X'
  
      if c == peg3:
          g3 = 'R'
      elif c == peg1:
          g3 = 'W'
      elif c == peg2:
          g3 = 'W'
      elif c == peg4:
          g3 = 'W'
      else:
          g3 = 'X'
  
      if d == peg4:
          g4 = 'R'
      elif d == peg1:
          g4 = 'W'
      elif d == peg2:
          g4 = 'W'
      elif d == peg3:
          g4 = 'W'
      else:
          g4 = 'X'
  
      print g1, g2, g3, g4
      global trial
      trial = trial + 1
      return trial
  
  
  
  colour = ['B', 'G', 'Y', 'P', 'R']
  
  peg1 = random.choice(colour)
  peg2 = random.choice(colour)
  peg3 = random.choice(colour)
  peg4 = random.choice(colour)
  g1 = 0
  g2 = 0
  g3 = 0
  g4 = 0
  
  print ""
  while g1 != 'R' or g2 != 'R' or g3 != 'R' or g4 != 'R':
      geuss()
  print "Congratulations! It took you %d tries to crack the code!" % trial
  
  print ""
  print "The code was %s%s%s%s." % (peg1, peg2, peg3, peg4)
  </code></pre>
  
  <p>As you can see the if and elif statements in the function 'geuss()' are needlessly wrong- but when I tried putting them together the script would always put a W.</p>
  
  <pre><code>    if a == peg1:
          g1 = 'R'
      elif a == peg2 or peg 3 or peg4:
          g1 = 'W'
      else:
          g1 = 'X'
  </code></pre>
  
  <p>Even when I put "QWER" in as an input, I would get an X. Is there some way I can consolidate them while still getting the correct response?</p>
  
  <p>Also, off topic, if there are any other suggestions you can give me on the script as I am a beginner, that would be much appreciated! Thank you!</p>