Thomas
Thomas

Reputation: 3

Python List Index Out of Range Lists

I am new to programming and was creating a portion of a program for this project: http://www.reddit.com/r/beginnerprojects/comments/19ot36/project_a_variation_of_21/ when I ran into a index error. Using Stackoverflow answers, I originally corrected the error by making

random.randrange(0,len(cards)+1)]

instead of

random.randrange(0,53,1)]

However it continues to give me this error. If you run it 50 times it might not give you an error, but it might give you an error the first or fifth time you run it too. For the round function, I want to be able to deal cards from a single deck, each time I draw a card it removes it from the deck. Any advice would be most appreciated! - Thomas

Error Message:

Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "<module2>", line 39, in <module>
  File "<module2>", line 30, in round
  File "<module2>", line 26, in draw

IndexError: list index out of range

Code:

def round():
    cards = ["2", "2", "2", "2", "3", "3", "3", "3", "4", "4", "4", "4", "5",
    "5", "5", "5", "6", "6", "6", "6", "7", "7","7","7","8", "8", "8", "8", "9",
    "9", "9", "9", "10", "10", "10", "10", "Jack", "Jack", "Jack", "Jack",
    "Queen", "Queen", "Queen", "Queen", "King", "King", "King", "King", "Ace", "Ace",
    "Ace", "Ace"]
    def draw():
        return cards[random.randrange(0,len(cards)+1,1)]
    acards = []
    aroundscore = 0
    acards.append(draw())
    acards.append(draw())
    print(acards)
    print(acards)
    cardsvalues = {"2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9,
    "10":10, "Jack":10, "Queen":10, "King":10, "Ace":1}
    for i in acards:
        print(cardsvalues[i])

Upvotes: 0

Views: 202

Answers (4)

VladimirM
VladimirM

Reputation: 817

I agree with above suggestions. But here is one more thing: randrange does not guarantee that generated numbers will be different. You can get a few numbers of the same value. It is very bad for card game - because every card in a game must be unique. I'd suggest to use random.shuffle and take cards from the list one by one from the beginning.

Upvotes: 0

xZel
xZel

Reputation: 60

Use random.randit(0,51) I think you are confused with the length of cards.

Upvotes: 0

voithos
voithos

Reputation: 70552

The error is occurring in your draw function because your range is too large: random.randrange(0,len(cards)+1,1). Think about, for example if len(cards) was 3. Then, the rangerange would be called with 0, 4, 1, which means that it will return integers between 0 and 3. But the index 3 wouldn't exist - it's an off-by-one error.

I'd recommend just using random.choice to accomplish what you want. It's much more concise and readable.

def draw():
    return random.choice(cards)

Upvotes: 1

Ryan Saxe
Ryan Saxe

Reputation: 17829

The reason you use len(cards) is because the amount of cards could change if you deal a card! your line should be len(cards) not len(cards) + 1 because if you add one to the maximum length of cards and it can then randomly pick a number that will be larger than the amount of cards you have, thereby causing an IndexError

Upvotes: 1

Related Questions