How could I shorten this loop with 4 redundant if statements?
This code is meant to count how many of each suit is in a player's hand in a card game:
suitcounter = [0, 0, 0, 0]
if len(hand) > 0:
for card in hand:
if card[1] == "C":
suitcounter[0] += 1
if card[1] == "D":
suitcounter[1] += 1
if card[1] == "S":
suitcounter[2] += 1
if card[1] == "H":
suitcounter[3] += 1
return suitcounter
Example: Hand consists of two hearts and one spade:
>>>hand = ['3H', '4H', 'AS']
[0, 0, 1, 2]
3H = 3 of hearts, 4H = 4 of hearts, AS = Ace of spades.
I feel there's just too much code 'spam' in what I've done. WTB tips.
Aucun commentaire:
Enregistrer un commentaire