0

Is there any way to assign multiple values to a variable? I am coding a solitaire game and using the values of ACE through KING for the cards. Is there any way to assign a variable the value of "ACE" and the value of "1" and so on, KING being "13"? This is not necessary, but would make the code a lot simpler and easier to create.


Thank you all, I currently have this:

from collections import namedtuple
Card = namedtuple('Card', 'name value')
ace = Card(name="ACE", value=1)

When I do: print(ace) I end up with:Card(name="ACE", value=1) and when I do ace.name or ace.value, I get the right outcome as well.

Is there any way to have the "ace" value in a list, and withdraw just the name?

1

3 Answers 3

4

Use a dictionary.

{'ace' :1, 'two' :2', ...etc}
Sign up to request clarification or add additional context in comments.

Comments

4

You could use a class or namedtuple to wrap both pieces of data

from collections import namedtuple

Card = namedtuple('Card', 'name value')
ace = Card(name="ACE", value=1)
king = Card(name="King", value=13)

A custom class might be better because then you can implement logic like card1 < card2

could I make the statement stackA + 1 == stackB true?

Yes, if you implement an __radd__ and __eq__ methods on the Card type that accept adding other Cards to int types and comparing the results to other Card types

6 Comments

cricket_007 if i did this would i be able to use the value "ACE" and the value "1" seperately?
Yes. ace.name or ace[0], as mentioned in the link to the docs will produce "ACE"
how would i recall the value idea from the namedtuple?
Same way, different field/index.
If i had a list: listA = [ace, "fake", "fake"] ... When I do "print(listA[0])", I get "Card(name="ACE", value=1)"... Is there anyway to just recall the name or just the value?
|
0

You can use a custom class:

class Card(object):
    # fancier output
    s = {0:"hearts", 1:"diamonds", 2:"clubs", 3:"spades"}
    r = {i:v for i,v in enumerate( [ "Ace","2","3","4","5","6","7","8",
                                     "9","10","Jack","Queen","King"],1)}
    def __init__(self, suit,rank):
        self.suit = suit
        self.rank = rank

    def __str__(self):
        return "{} of {}".format(Card.r[self.rank],Card.s[self.suit])

    def __repr__(self):
        return str(self)

    def __lt__(self,other):
        return (self.suit,self.rank) < (other.suit, other.rank)


    @classmethod
    def summ_hand(cls,hand):
        return sum ( 10 if card.rank > 10 else card.rank for card in hand)


class Deck(object): 
    def __init__(self):
        self.cards = [Card(suit,rank) for suit in range(4) for rank in range(1,14)]


deck = Deck()

print( deck.cards )

Output:

[Ace of hearts, 2 of hearts, 3 of hearts, 4 of hearts, 5 of hearts, 6 of hearts, 
 7 of hearts, 8 of hearts, 9 of hearts, 10 of hearts, Jack of hearts, Queen of hearts, 
 King of hearts, 
 Ace of diamonds, 2 of diamonds, 3 of diamonds,4 of diamonds,5 of diamonds, 6 of diamonds, 
 7 of diamonds, 8 of diamonds, 9 of diamonds, 10 of diamonds, Jack of diamonds, 
 Queen of diamonds, King of diamonds, 
 Ace of clubs, 2 of clubs, 3 of clubs, 4 of clubs, 5 of clubs, 6 of clubs, 7 of clubs, 
 8 of clubs, 9 of clubs, 10 of clubs, Jack of clubs, Queen of clubs, King of clubs, 
 Ace of spades, 2 of spades, 3 of spades, 4 of spades, 5 of spades, 6 of spades, 
 7 of spades, 8 of spades, 9 of spades, 10 of spades, Jack of spades, Queen of spades, 
 King of spades]

Providing __lq__(self) allows you to sort:

hand = random.choices(deck.cards, k=7)
print(hand)
print(sorted(hand))

print( Card.summ_hand([Card(0,9),Card(0,11),Card(0,13)]))

Output:

# as given by choices
[Ace of hearts, Ace of spades, Queen of diamonds, 9 of spades, 
 King of hearts, Ace of spades, 3 of diamonds]

 # sorted
[Ace of hearts, King of hearts, 3 of diamonds, Queen of diamonds,
 Ace of spades, Ace of spades, 9 of spades]

29  # summ_hand for 9,Queen,King, 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.