summer
summer

Reputation: 13

IndexError: list index out of range in Python 3

I am newbie to Python. I got the index error when I run the code. I have seen the relevant questions in Stackoverflow, but I still can't see what the bug is. I am very appreciated for any response. Thank you. Here is the code:

 class Card:
   def __init__(self, suit = 0, rank = 2):
       self.suit = suit
       self.rank = rank

   suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
   rank_names =[None,'Ace','2','3','4','5','6','7','9','10','Jack','Queen', 'King']

   def __str__ (self):
       return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit])

   def __lt__(self,other):
       t1 = self.suit, self.rank
       t2 = other.suit, other.rank
       return t1 < t2


class Deck:
   def __init__(self):
       self.cards = []
       for suit in range(4):
           for rank in range(1, 14):
               card = Card(suit, rank)
               self.cards.append(card)

   def __str__(self):
       res = [ ]
       for card in self.cards:
           res.append(str(card))
       return '\n'.join(res)


deck1 = Deck()
print(deck1)

Then I got the following error:

   Traceback (most recent call last):
  File "/Users/Enze/Python/untitled/Inheritance.py", line 35, in <module>
    print(deck1)
  File "/Users/Enze/Python/untitled/Inheritance.py", line 30, in __str__
    res.append(str(card))
  File "/Users/Enze/Python/untitled/Inheritance.py", line 11, in __str__
    return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit])
IndexError: list index out of range

Upvotes: 0

Views: 671

Answers (3)

hrishi
hrishi

Reputation: 433

use are missing '8' in rank_names

class Card:
   def __init__(self, suit = 0, rank = 2):
       self.suit = suit
       self.rank = rank

   suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
   rank_names =[None,'Ace','2','3','4','5','6','7','8','9','10','Jack','Queen', 'King']

   def __str__ (self):
       return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit])

   def __lt__(self,other):
       t1 = self.suit, self.rank
       t2 = other.suit, other.rank
       return t1 < t2


class Deck:
   def __init__(self):
       self.cards = []
       for suit in range(4):
           for rank in range(1, 14):
               card = Card(suit, rank)
               self.cards.append(card)

   def __str__(self):
       res = [ ]
       for card in self.cards:
           res.append(str(card))
       return '\n'.join(res)


deck1 = Deck()
print(deck1)

Upvotes: 0

MooingRawr
MooingRawr

Reputation: 5001

class Card:
   def __init__(self, suit = 0, rank = 2):
       self.suit = suit
       self.rank = rank

   suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
   rank_names =[None,'Ace','2','3','4','5','6','7','9','10','Jack','Queen', 'King']

   def __str__ (self):
       return '%s of %s' % (Card.rank_names[self.rank], Card.suit_names[self.suit])

   def __lt__(self,other):
       t1 = self.suit, self.rank
       t2 = other.suit, other.rank
       return t1 < t2


class Deck:
   def __init__(self):
       self.cards = []
       for suit in range(4):
           for rank in range(1, 13): #error here.
               card = Card(suit, rank)
               self.cards.append(card)

   def __str__(self):
       res = [ ]
       for card in self.cards:
           res.append(str(card))
       return '\n'.join(res)


deck1 = Deck()
print(deck1)

You had 13 cards but you tried to find 14 cards causing an out of index issue.

Take this example:

for i in range(1,14):
    print(i)

It prints 1 to 13 including 13. But in your list index starts at 0 so 13 items would give you 0-12 slots including 0.

Upvotes: 2

m.antkowicz
m.antkowicz

Reputation: 13581

You have 13 items inside your rank_names list so the index of last element is of value 12 (lists are enumerated starting with 0) - inside loop

for suit in range(4):
       for rank in range(1, 14):
           card = Card(suit, rank)

the maximum index you try to get is 13 (range is generating numbers one-by-one in order excluding borders) and that's why you've got index out of range exception

Upvotes: 3

Related Questions