简体   繁体   中英

Creating a basic Blackjack game in Python - struggling to assign and total card values

I apologise in advance for absolute novice I am.

So this was my attempt at assigning values to each card (the program is picking from a separate .txt file that has the list of cards in it)

cardAvalue = int   
if cardA == 'Jack of Spades' or 'Queen of Spades' or 'King of Spades' or '10 of Spades' or 'Jack of Hearts' or 'Queen of Hearts' or 'King of Hearts' or '10 of Hearts' or 'Jack of Clubs' or 'Queen of Clubs' or 'King of Clubs' or '10 of Clubs' or  'Jack of Diamonds' or 'Queen of Diamonds' or 'King of Diamonds' or '10 of Diamonds':
         cardAvalue = int(10)
elif cardA == '2 of Spades' or '2 of Hearts' or '2 of Clubs' or '2 of Diamonds':
         cardAvalue = int(2)
elif cardA == '3 of Spades' or '3 of Hearts' or '3 of Clubs' or '3 of Diamonds':
         cardAvalue = int(3)
elif cardA == '4 of Spades' or '4 of Hearts' or '4 of Clubs' or '4 of Diamonds':
        cardAvalue = int(4)
elif cardA == '5 of Spades' or '5 of Hearts' or '5 of Clubs' or '5 of Diamonds':
         cardAvalue = int(5)
elif cardA == '6 of Spades' or '6 of Hearts' or '6 of Clubs' or '6 of Diamonds':
         cardAvalue = int(6)
elif cardA == '7 of Spades' or '7 of Hearts' or '7 of Clubs' or '7 of Diamonds':
         cardAvalue = int(7)
elif cardA == '8 of Spades' or '8 of Hearts' or '8 of Clubs' or '8 of Diamonds':
         cardAvalue = int(8)
elif cardA == '9 of Spades' or '9 of Hearts' or '9 of Clubs' or '9 of Diamonds':
         cardAvalue = int(9)
elif cardA == ('Ace of Spades' or 'Ace of Hearts' or 'Ace of Clubs' or 'Ace of Diamonds'):
         cardAvalue = int(11)
cardCvalue = int
if cardC == 'Jack of Spades' or 'Queen of Spades' or 'King of Spades' or '10 of Spades' or 'Jack of Hearts' or 'Queen of Hearts' or 'King of Hearts' or '10 of Hearts' or 'Jack of Clubs' or 'Queen of Clubs' or 'King of Clubs' or '10 of Clubs' or  'Jack of Diamonds' or 'Queen of Diamonds' or 'King of Diamonds' or '10 of Diamonds':
        cardCvalue = int(10)
elif cardC == '2 of Spades' or '2 of Hearts' or '2 of Clubs' or '2 of Diamonds':
        cardCvalue = int(2)
elif cardC == '3 of Spades' or '3 of Hearts' or '3 of Clubs' or '3 of Diamonds':
        cardCvalue = int(3)
elif cardC == '4 of Spades' or '4 of Hearts' or '4 of Clubs' or '4 of Diamonds':
        cardCvalue = int(4)
elif cardC == '5 of Spades' or '5 of Hearts' or '5 of Clubs' or '5 of Diamonds':
        cardCvalue = int(5)
elif cardC == '6 of Spades' or '6 of Hearts' or '6 of Clubs' or '6 of Diamonds':
        cardCvalue = int(6)
elif cardC == '7 of Spades' or '7 of Hearts' or '7 of Clubs' or '7 of Diamonds':
        cardCvalue = int(7)
elif cardC == '8 of Spades' or '8 of Hearts' or '8 of Clubs' or '8 of Diamonds':
        cardCvalue = int(8)
elif cardC == '9 of Spades' or '9 of Hearts' or '9 of Clubs' or '9 of Diamonds':
        cardCvalue = int(9)
elif cardC == ('Ace of Spades' or 'Ace of Hearts' or 'Ace of Clubs' or 'Ace of Diamonds') and (cardAvalue <= 10):
        cardCvalue = int(11)
elif cardC == ('Ace of Spades' or 'Ace of Hearts' or 'Ace of Clubs' or 'Ace of Diamonds') and (cardAvalue > 10):
        cardCvalue = int(1)
cardA = cardAvalue
print(cardAvalue)

Both cardAvalue and cardCvalue both keep coming out as 10, the number of the variable given, despite me clearly failing to assign numerical values. Sorry for the excessive int() tags given, I've been messing with my code in frustration.

I'm trying to work out the numerical total of the player's hand. Where am I going wrong/What would be the best way of doing this?

You can probably do this without a function, but I have a feeling your next assignment will be to extend the functionality of the code to do something else. In the example below I tried to encode the rules you seem to be using.

  1. Discard the suit of the card, you don't seem to use that unless I missed something.
  2. Unless the score is already above some number score_threshold an Ace counts for 11, else 1.
  3. Face cards count for 10, number cards count for their number.

So then we can encode those rules in a function:

def process_cards(cards, score_threshold):
      score = 0
      face_cards = ["Jack", "Queen", "King"]
      for x in cards:
        card = x.split()[0] # Discard suit
        if card in face_cards: # Face cards are ten
          score = score + 10
        elif card == "Ace":
          # Score based on score_threshold arg
          if score <= score_threshold:
            score = score + 11
          else:
            score = score + 1
        # If it's a digit, pass through that number to score
        elif card.isdigit():
          score = score + int(card)
        # Everything else is an error case
        else: 
          print("Invalid card!")
      # Make the score accessible outside the function
      return score

p = process_cards(["Queen of Spades", "Ace of Hearts"], 9)
print(p) # 11

At then end we can assign the return value of that function to a variable and then use it for whatever comes next.

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