0

My program involves drawing a card from a deck, adding the value of it to my hand and then deleting the card from the deck (since I don't want to draw the same card twice).

Example of a card:

card = ["4 of clubs", 4]

The code that doesn't work:

card_number = random.randrange(len(deck_cards))
card = deck_cards[card_number][0]
print(card)
hand_value += deck_cards[card_number][1]
deck_cards.remove(card_number)

I have no idea what I am doing wrong, and if I change the code to

card_number = random.chioce(deck_cards)

it won't accept the card = deck_cards[card_number][0] part.

What to do?

1
  • deck_cards.remove(card_number) should be deck_cards.remove(deck_cards[card_number]) The parameter is the element to remove, not its index. Try typing help(list.remove) in the REPL. It will tell you, L.remove(value) -> None -- remove first occurrence of value. Commented Oct 22, 2015 at 12:55

2 Answers 2

2

list.remove takes an object, not an index, as parameter.

So you should modify your code to retrieve a card and use it directly:

>>> card = random.choice(deck_cards) # Retrieve an object, not an index
>>> print (card) # card is you card list, i.e.:
["4 of clubs", 4]
>>> print (card[0])
4 of clubs
>>> hand_value += card[1]
>>> deck_cards.remove (card)

Note that you can also delete an object at a specified index using the del built-in:

>>> card_number = random.randrange(len(deck_cards))
>>> card = deck_cards[card_number][0]
>>> print(card)
"4 of clubs"
>>> hand_value += deck_cards[card_number][1]
>>> del deck_cards[card_number]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That works, but is there any way I can have the code disply only the first part?
@JesperVines Simply print(card[0]) instead of print(card).
0

random.choice(list) returns a random element of the list, but the list deck_cards[card_number] expects the position of the item to retrieve from the list (hence an integer). So just stating card = random.choice(deck_cards)[0] is sufficient.

Also, using a list as reprensentation of a card is a poor choice, because it does not represent the object characteristic of a card. If you wrote you own class for cards, your code would become much more readable. If the class would look like this:

class Card(object):
    def __init__(self, name, value):
        self.name = name
        self.value = value

and you instantiated a card like card = Card('4 of clubs', 4), you could write code like hand_value = deck_cards[index].value and get rid of those ugly indices. Of curse you would have to write you own string presenter for this class for easy fancy output of a card, but that's not to hard either.

So (without that string representational code) you problem would be solved like this:

card = random.choice(deck_cards)
print('{},{}'.format(card.name, card.value))
hand_value += card.value
deck_cards.remove(card)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.