title: Infinite Loop in War Card Game Simulator
body: <p>I'm trying to simulate a (tiny) game of War! as an exercise in OOP. I have a loop that plays two decks against each other, but it seems to loop forever, repeating the same sequence again and again. The output gets to the point where one deck has 4 card objects in it, and then the loop repeats. The loop should only be running when both Drawpile objects are greater than zero. </p>
  
  <pre><code>ranks = [2,3,4,5,6,7,8,9,10,11,12,13,14] #for use of value comparison via index
  suits =['Diamonds','Hearts','Clubs','Spades']
  rounds = 0
  
  class Card:
      def __init__(self, rank, suit): 
          self.rank = rank
          assert rank in ranks
          assert suit in suits
          self.suit = suit
      def __repr_(self):
          return '{} of {}'.format(self.rank,self.suit)
      def __str__(self):
          return '{} of {}'.format(self.rank,self.suit)
      def __eq__(self, other): 
          if self.rank == other.rank:
              return True
          else:
              return False
      def __gt__(self, other): #gt compared via ordering of ranks index
          if self.rank &gt; other.rank:
              return True
          else:
              return False
      def __lt__(self, other):
          if self.rank &lt; other.rank:
              return True
          else:
              return False
  
  class Drawpile:
      def __init__(self, pile =[]):
          self.pile = pile
      def __len__(self):
          return len(self.pile)
      def __getitem__(self, n):
          return self.pile[n]
      def add(self, n):
          self.pile.append(n)
      def draw(self):
          if len(self) &lt;= 0:
              return None
          return self.pile.pop(0)
  
  dp1 = Drawpile([Card(2,'Diamonds'),Card(13,'Hearts'),Card(7,'Clubs')])
  dp2 = Drawpile([Card(4,'Hearts'),Card(3,'Hearts'),Card(8,'Spades')])
  
  while len(dp1) &gt; 0 and len(dp2) &gt; 0:
  
      rounds += 1
      c1 = dp1.draw()
      c2 = dp2.draw()
  
      print('Round ',rounds)
      print('c1',c1, len(dp1))
      print('c2',c2, len(dp2))
  
      if c1 &gt; c2:
          dp1.add(c1)
          dp1.add(c2)
      else:
          dp2.add(c1)
          dp2.add(c2)
  
  if len(dp1) &gt; len(dp2):
      print('Pile 1 Won!')
  else:
      print('Pile 2 Won!')
  </code></pre>
  
  <p>Can anyone tell me where exactly this loop is getting stuck? I've tried diagramming it out and this is my second rewrite. Any ideas?</p>