title: Python: Cows and Bulls game
body: <p>I am writing a code fora game which randomly generates a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly
  in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every time the user makes a guess, tell them how many “cows” and “bulls” they have. Once the user guesses the correct number, the game is over. Keep track of the number of guesses the user makes throughout the game and tell the user at the end.</p>
  
  <p>The code is below.</p>
  
  <pre><code>import random
  
  rand=[random.randint(0,9) for n in range(3)]
  
  
  user_guess=[input("Please guess 4-digit number: ")]
  
  def game():
      count=0
      while True:
          guess=[i for i in rand]
          listnum=[i for i in user_guess]
  
          if guess == listnum:
              print("You won.")
              print("It took you "+str(count)+" guess.")
              break
  
          if guess != listnum:
              cow=0
              bull=0
              count+=1
              for x in range(0,3):
                  if guess[x]==listnum[x]:
                      cow+=1
  
          if len(set(guess)&amp;set(listnum))&gt;num:
              bull=len(set(guess)&amp;set(listnum)) - cow
  
          print("Cows: "+str(cow)+' Bulls: '+str(bull))
  
  game()
  </code></pre>
  
  <p>But I am getting the following error after it asks the user to guess the number and takes the input. The error is given below.</p>
  
  <pre><code>Please guess 4-digit number: 1234
  
  ---------------------------------------------------------------------------
  IndexError                                Traceback (most recent call last)
  &lt;ipython-input-48-64a0b0d4d766&gt; in &lt;module&gt;()
       41         print("Cows: "+str(cow)+' Bulls: '+str(bull))
       42 
  ---&gt; 43 game()
       44 
       45 
  
  &lt;ipython-input-48-64a0b0d4d766&gt; in game()
       33             count+=1
       34             for x in range(0,3):
  ---&gt; 35                 if guess[x]==listnum[x]:
       36                     cow+=1
       37 
  
  IndexError: list index out of range
  </code></pre>
  
  <p>What mistake am I making here?</p>