best way to implement a deck for a card game in python

前端 未结 5 413
执念已碎
执念已碎 2020-12-31 05:06

What is the best way to store the cards and suits in python so that I can hold a reference to these values in another variable?

For example, if I have a list called

5条回答
  •  盖世英雄少女心
    2020-12-31 05:52

    import collections
    
    C, H, D, S = "CLUBS", "HEARTS", "DICE", "SPADE"
    Card = collections.namedtuple("Card", "suit value")
    
    hand = []
    
    hand.append(Card(C, 3))
    hand.append(Card(H, "A"))
    hand.append(Card(D, 10))
    hand.append(Card(S, "Q"))
    
    for card in hand:
        print(card.value, card.suit)
    

提交回复
热议问题