1

I have a list and I want to assign a random element from that list to a new variable five times in order to generate a hand of cards. I've searched, and I can't seem to understand why my random.choice is causing errors.

import random

cards = { '2' '3' '4' '5' '6' '7' '8' '9' 'J' 'Q' 'K' 'A' }

for newCard in range(5):
        newDraw = random.choice(cards)
        playerdeck.append(newDraw)
        if newDraw in playerdeck:
                playermatches = playermatches + 1
        newDraw = oldDraw

When I try to run it I get this error:

Traceback (most recent call last):
  File "C:\Users\Nathaniel\Desktop\Fcard.py", line 6, in <module>
    newDraw = random.choice(cards)
  File "C:\Python32\lib\random.py", line 253, in choice
    return seq[i]
TypeError: 'set' object does not support indexing
5
  • 1
    The answer that you have accepted doesn't work. Commented May 17, 2011 at 3:10
  • @John Machin: I realized that my list wasn't being defined correctly at all. cards = [ '2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K', 'A' ] This is what I wanted and it is working correctly.
    – Subtlebot
    Commented May 17, 2011 at 3:15
  • There are two answers that also pointed out that you didn't have a list and (a) were submitted before the one that you chose and (b) did not contain non-working code. Commented May 17, 2011 at 3:21
  • @John Machin: I'm confused. The answer I chose was submitted first and helped me solve my problem immediately. Isn't that grounds for me to choose it? I didn't realize that my list wasn't defined correctly. That was the root of my problem.
    – Subtlebot
    Commented May 17, 2011 at 3:34
  • Sorry, I was confused about the order of the answers. Commented May 17, 2011 at 3:40

3 Answers 3

2

You used { and } rather than [ and ] to define cards. Therefore, it is a set. sets are not ordered, and therefore it makes no sense to try to index into it. random.choice needs to index into a collection to pull an item out of it. Anyway, a quick fix is to replace this:

random.choice(cards)

With this:

random.choice(list(cards))

Edit: Also add commas between the elements in the set.

2
  • 1
    -1 This won't work. The set has only 1 element. The quickest fix is to remove the braces. Commented May 17, 2011 at 3:03
  • I feel blind after staring at everything for so long. My list wasn't a list at all! Thank you for pointing that out, I hadn't even realized it.
    – Subtlebot
    Commented May 17, 2011 at 3:12
1

So use a list:

>>> cards = ['2', '3', '4', '5', '6', '7', '8', '9', 'J', 'Q', 'K', 'A']
>>> random.choice(cards)
'9'
1

There's a problem with your set definition. When you have multiple strings side by side, python concatenates them as if you did 'A' + 'B'. So you generated a set with only one element which is '23456789JQKA'

You don't really need a set or a list to do that:

>>> cards = '23456789JQKA'
>>> random.choice(cards)

PS: Although sets cannot be indexed, they can be iterated:

>>> [int(i) for i in {'1','2','3'}]
[1, 3, 2]

Note the order may not be preserved.

1
  • 1
    +1 for pointing out that the set has only one element, and for suggesting a string instead of a list. Commented May 17, 2011 at 3:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.