0

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?

Eb946207
  • 748
  • 8
  • 26
  • Do not use code snippets for python. That is for HTML, CSS, and Javascript code only. – Eb946207 Dec 18 '18 at 23:18
  • 1
    Python logic does not work like English language. `elif cardC == '6 of Spades' or '6 of Hearts' or '6 of Clubs' or '6 of Diamonds':` and all the rest are _always_ True because strings are truthy. `elif cardC == '6 of Spades' or cardC == '6 of Hearts' or cardC...` – roganjosh Dec 18 '18 at 23:20
  • Also, the setting to `int` at the beginning and the calls of `int()` are not needed. – Eb946207 Dec 18 '18 at 23:21
  • 1
    Possible duplicate of [Why does \`a == b or c or d\` always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true) – Thierry Lathuille Dec 18 '18 at 23:21
  • 1
    Anyway, that's going to be horrendous to write out. I've not tried to follow your logic all the way through, but a 10 second improvement might be `elif cardC.split()[0] == '2':` etc. Or even just `else: cardAvalue = int(cardA.split()[0])` – roganjosh Dec 18 '18 at 23:22
  • https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value – Eb946207 Dec 18 '18 at 23:23
  • Have you read or looked into dictionaries? It Might be a better way to implement this. – chitown88 Dec 18 '18 at 23:55
  • Wow. I would be hard-pressed to come up with a design that wasted more time and memory. Use numbers! Computers like numbers. They're fast, small, and versatile. Only convert to strings when you output to a dumb human. – Lee Daniel Crocker Dec 19 '18 at 22:22

1 Answers1

0

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.

Charles Landau
  • 4,187
  • 1
  • 8
  • 24