Reputation: 167
I'm creating a card game using Python. I've represented my cards as 5D
, 10H
, etc. So far I've used a .pop()
approach to take cards from a deck [list] to the player's hand [list]. This was working great, until I created another list to act as the discard pile. When a player drops a card, I pop it into the discard list but it splits the characters into separate elements. No other list has done this throughout the program.
Example of what happens:
discard = []
hand = ['12D', '5C', '3D']
discard += hand.pop(0)
discard = ['1', '2', 'D']
How can I prevent this?
Upvotes: 2
Views: 73
Reputation: 168616
+=
adds a sequence to a list and is an alias for list.extend()
. You want to add an individual element to the list, so +=
is not the correct operation for you.
Try this:
discard.append(hand.pop(0))
The Python documentation has a convenient table that explains all of the operations on lists and other mutable sequence types.
Upvotes: 3