0

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])

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

beat me by seconds :)
That fixed it. Thank you Ryan and Hammer!
1

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)

3 Comments

Thanks voithos. I like the concept of using random.choice instead of random.randrange(len(cards)) as it seems much cleaner as less room for error. It looks like random.choice needs to have a sequence of int's though
@Thomas: Actually, it doesn't. If you look at the documentation, it states that random.choice just returns a random element from a non-empty sequence. If you use it on cards, it will return one of the strings inside.
Ah I see, yes implemented like that does work. Thank you again!
0

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

Comments

0

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.

1 Comment

You're right. I modified my draw function to this: def draw(): thisdraw = cards[random.randrange(0,len(cards))] cards.remove(thisdraw) return thisdraw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.