How do I check if multiple returned class objects have the same value for comparison and edit only one, while keeping both for addition later

I’m currently creating a blackjack card game using OOP and the blessed library. As it stands I create the card objects which return a class object to me:

player_card_data = [<blackjack.card.Card object at 0x7f3900226330>, <blackjack.card.Card object at 0x7f3900225f70>]

I can then dive into those and get the card value:

player_card_data[0].card_value

This, for example, gives me 10.

My current issue is I want to account for aces, which earlier in my program I set to the value 11. I need to check and see what the score is so that the hand does not automatically go bust by drawing 2 aces (22), I’m currently building it into a reusable function and figure I’d need to do a simple compariosn operator.

(This is also currently what I am trying / have tried and so far no results.)

def calculate_score(player_card_data):
  """
  Calculates the score of the players cards, accounting for aces, then returns the score
  """
  if player_card_data[0].card_value == player_card_data[1].card_value:
    player_card_data[0].card_value = 1

  player_score = player_card_data[0].card_value + player_card_data[1].card_value
  return player_score

The problem I see with this while it works with two cards, it doesn’t scale and completely breaks if more than 3 cards are in the player_card_data object. I was thinking of using *args but i’m not sure if this would work / have not been able to get it working in this situation. I am not looking to make it into a set as I need both values for addition later in the function.

The card objects are initially populated into a deck array, which the cards are then drawn from, then deleted from this deck. This ensures that the objects i’m manipulating only exist for the round of blackjack being played and as a result won’t affect the deck as after the current round, no longer exist. I am aware that manipulating objects is generally bad practice but if the object no longer exists after the round figured it isn’t a severe issue.

I do it like this to create a finite number of plays before the deck needs to be “reshuffled” (recreated).

The data that’s fed in to create the card objects is housed outside the card class inside objects and arrays:

suits = ["Spades", "Hearts", "Clubs", "Diamonds"]
suits_values = {"Spades":"u2664", "Hearts":"u2661", "Clubs": "u2667", "Diamonds": "u2662"}
cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
cards_values = {"A": 11, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "10":10, "J":10, "Q":10, "K":10}

which is then sent to the card class and populated in the deck to make the 52 unique card objects:

def create_deck():
  """
  Creates a new deck of 52 card objects by passing each card suit, card and card value
  into the Card class, then appending it to the deck array.
  """
  # we now create our deck of cards
  deck = []
  # Loop for every type of suit
  for suit in suits:
    cards = ["A", "A", "A", "A"]
    # Loop for every type of card in a suit
    for card in cards:
      # Adding card to the deck
      deck.append(Card(suits_values[suit], card, cards_values[card]))

  return deck

The cards once “drawn” in the round are removed completely from the deck, im looking to find a way to calculate the score based on if the player or CPU draws multiple “Ace” (11) card objects. I do this because I want to draw the cards onto the terminal with blessed later on, so I need to store them in an object to access later.

How could I go about making this scalable for any number of cards?

11

An alternative approach is to consider every ace as having value 1, and if the sum of a hand containing at least one ace is 11 or less, add a bonus of 10. This works because at most one of the aces can be assigned a value of 11 without exceeding 21.

1

Final Edit:

Found an answer to the problem, thanks to the comments for helping me course correct on where I was looking. Instead of trying to manipulate the objects to get the data, which even if it’s being removed still is mutating data which is always bad practice I settled for this instead.

def calculate_score(player_card_data):
  """
  Function that loops over the player_card_data and adds the score to a score 
  array the function also accounts for multiple aces placed into the array by 
  checking if there is already 11 in the array.
  """
  player_score = []
  for i in player_card_data:
    if 11 in player_score:
      player_score.append(1)
    else: 
      player_score.append(i.card_value)
    Sum = sum(player_score)
  return Sum

This will always check to see if an “Ace” (11) is present in the array and if one is, just append a 1 to the array instead.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật