简体   繁体   中英

How to sum a list that contains letters by changing certain letters in a list with integers?

Just started learning Python and I am trying to create a blackjack game. I want to find the sum of the card values in a player's hand. My approach was to change the letters "J", "Q","K" into values of 10 and change "A" to value of 11. I have defined the class Card but when I run the code, I get an error saying "type object 'Card' has no attribute 'value'"


import random


DEFAULT_SUITS = ("Clubs","Hearts","Diamonds","Spades")
DEFAULT_VALUES = ("A",2,3,4,5,6,7,8,9,10,"J","Q","K")


class Card():
    def __init__ (self, suit, value):
        self.suit = suit
        self.value = value
    
    def show(self):
        print(self.value, self.suit)

  
class Shoes():
    def __init__(self, decks, suits=DEFAULT_SUITS, values=DEFAULT_VALUES):
        self.cards = []
        for deck in range(decks):
            for suit in suits:
                for value in values:
                    self.cards.append(Card(suit,value))
        random.shuffle(self.cards) 
        
    def show(self):
        for card in self.cards:
            card.show()
    
    def drawcard(self):
        return self.cards.pop()
    
          
class Player():
    def __init__ (self, name):
        self.name = name
        self.hand = []
    
    def drawcard(self, shoes):
        self.hand.append(shoes.drawcard())
        return self
    
    def totalvalue(self):
        for card in self.hand:
            if Card.value == "J":
                self.hand = [10 if card=="J" else card for card in cards]
            if Card.value == "Q":
                self.hand = [10 if card=="Q" else card for card in cards]
            if Card.value == "K":
                self.hand = [10 if card=="K" else card for card in cards]
            if Card.value == "A":
                self.hand = [11 if card=="A" else card for card in cards]    
        self.totalvalue = sum(self.hand(Card.value))
    
    def showhand(self):
        for card in self.hand:
            card.show()
            
shoe = Shoes(int(input("Enter the number of deck used in a shoes: ")))

bob = Player("bob")
bob.drawcard(shoe)
bob.showhand()
bob.totalvalue()

How can I change the values "J","Q","K","A" in hand and sum it up to get the total value?

you have used Card.value, where Card is a class instace, just change Card to card in totalvalue function.

and next thing, i will suggest to make a dictionary; and take every value from that even numbers too.

dict = {'A':1, '2':2,.......}

and so on, like this

def totalvalue(self):
   self.totalvalue =0;
   dict = {'A':1, '2':2,.......};
       for card in self.hand:
            self.totalvalue = self.totalvalue +dict[card.value]

I see there is some confusion in using the list of Cards. Here what you should be doing

def totalvalue(self):
    total = 0
    for card in self.hand:
        if card.value in ["J", "Q", "K"]:
            total = total + 10
        elif card.value == "A":
            total = total + 11
        else:
            total = total + card.value
        
    self.totalvalue = total

Code can be further simplified using the list comprehension as follows

def totalvalue(self):
    return  sum([10 if card.value in ['J', 'Q', 'K'] else 11 if card.value == 'A' else card.value for  card in self.hand])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM