A simple form could be the following:
import random
palos = ["h","d","c","s"]
rangos = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
# Generamos el mazo de las 52 cartas
mazo = list(zip(palos * len(rangos), rangos* len(palos)))
jugador1 = []
jugador2 = []
# Desordenamos el mazo
random.shuffle(mazo)
while mazo:
jugador1.append(mazo.pop())
jugador2.append(mazo.pop())
print("Cartas jugador 2: {0}".format(jugador1))
print("Cartas jugador 2: {0}".format(jugador2))
In this example we do the following:
- With
list(zip(palos * len(rangos), rangos* len(palos)))
we generate a list of the whole deck combining clubs and ranks by zip()
.
- With
random.shuffle(mazo)
we mess up the deck randomly
- Then we simply go through the list as long as it has a letter and with the
pop()
method we obtain one and remove it from the list at the same time
- We keep two lists for each player and we add them as we ask for each card with
pop()
- This example completely deals the cards to the two players.
If we only want to distribute two cards, we should do something like this:
import random
palos = ["h","d","c","s"]
rangos = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
# Generamos el mazo de las 52 cartas
mazo = list(zip(palos * len(rangos), rangos* len(palos)))
jugador1 = []
jugador2 = []
# Desordenamos el mazo
random.shuffle(mazo)
jugador1.extend([mazo.pop(), mazo.pop()])
jugador2.extend([mazo.pop(), mazo.pop()])
print("Cartas jugador 2: {0}".format(jugador1))
print("Cartas jugador 2: {0}".format(jugador2))
What we do is "extend" the list of cards of each player with a new list of two elements by removing two cards from the deck for each player. Your original idea to keep a list of cards of the full deck is necessary since it is the only way to make sure that each letter we ask for is not one that we have already received.