-2

My return value is always printing out a total of 20 regardless of what cards are in the user_hand. Any reason as to why?

suits = ["Heart", "Diamond", "Spade", "Club"]
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

deck = [(suit, rank) for rank in ranks for suit in suits]

random.shuffle(deck,random.random)

user_hand = []
dealer_hand = []

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

def handtotal (hand):

    total = 0

    for rank in hand:

        if rank == "J" or "Q" or "K":

            total += 10

        elif rank == 'A' and total < 11:

            total += 11

        elif rank == 'A' and total >= 11:

            total += 1

        elif rank == '2':

            total += 2

        elif rank == '3':

            total += 3

        elif rank == '4':

            total += 4

        elif rank == '5':

            total += 5

        elif rank == '6':

            total += 6

        elif rank == '7':

            total += 7

        elif rank == '8':

            total += 8

        elif rank == '9':

            total += 9

    return total


print ("Your current hand is {}".format(user_hand))

print ("This provides you with a total of:")

print (handtotal(user_hand))
fredtantini
  • 15,966
  • 8
  • 49
  • 55
mtomney
  • 1
  • 2
  • `rank == "J" or "Q" or "K"` will return true, you want to do `rank in ["J" , "Q" , "K"]` – fredtantini Aug 19 '15 at 15:41
  • possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – fredtantini Aug 19 '15 at 15:41
  • You really should work on cutting down your error to the minimal possible code. That would have clued you in as to what line was incorrect. – Teepeemm Aug 19 '15 at 15:45
  • You'll also want to sort the ranks before assigning the value. `A, J, J` should be 21, not 31. And `J, A, A` should be 12, not 22, but that's a little more complicated. – Teepeemm Aug 19 '15 at 15:48

5 Answers5

1

One of your problems is this line:

if rank == "J" or "Q" or "K"

As it stands, this will always evaluate to true (well, technically "Q"). You should be comparing rank to all of the variables:

if rank == "J" or rank == "Q" or rank == "K"

Or the more Pythonic way:

if rank in ["J", "Q", "K"] 

In addition, you're passing user_hand to handtotal(). Since each element of user_hand is a tuple, you'll need to compare against second element of each element (e.g. rank[1]) of user_hand.

Sculper
  • 756
  • 2
  • 12
  • 24
0

You should change this:

if rank == "J" or "Q" or "K":

To this:

if rank in ["J", "Q", "K"]:

See operator precedence for more info.

Ethan Bierlein
  • 3,353
  • 4
  • 28
  • 42
-1

As people said, your first if is not ok.

Also, as I tried to run your code, I also noticed you're not comparing the rank, but the 'card' (the whole pair)

Your code working would be the following:

import random
suits = ["Heart", "Diamond", "Spade", "Club"]
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

deck = [(suit, rank) for rank in ranks for suit in suits]

random.shuffle(deck,random.random)

user_hand = []
dealer_hand = []

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

def handtotal (hand):

    total = 0

    for rank in hand:
        if rank[1] == "J" or rank[1] == "Q" or rank[1] == "K":
            total += 10
        elif rank[1] == 'A' and total < 11:
            total += 11
        elif rank[1] == 'A' and total >= 11:
            total += 1
        elif rank[1] == '2':
            total += 2
        elif rank[1] == '3':
            total += 3
        elif rank[1] == '4':
            total += 4
        elif rank[1] == '5':
            total += 5
        elif rank[1] == '6':
            total += 6
        elif rank[1] == '7':
            total += 7
        elif rank[1] == '8':
            total += 8
        elif rank[1] == '9':
            total += 9
    return total


print ("Your current hand is {}".format(user_hand))

print ("This provides you with a total of:")

print (handtotal(user_hand))
cfrag
  • 726
  • 6
  • 12
-1

This code works here:

import random
suits = ["Heart", "Diamond", "Spade", "Club"]
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

deck = [(suit, rank) for rank in ranks for suit in suits]

random.shuffle(deck,random.random)

user_hand = []
dealer_hand = []

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

user_hand.append(deck.pop())

dealer_hand.append(deck.pop())

def handtotal (hand):

    total = 0

    for rank in hand:
    rank = rank[1]
        if rank in ["J", "Q","K"]:

            total += 10

        elif rank == 'A' and total < 11:

            total += 11

        elif rank == 'A' and total >= 11:

            total += 1

        elif rank == '2':

            total += 2

        elif rank == '3':

            total += 3

        elif rank == '4':

            total += 4

        elif rank == '5':

            total += 5

        elif rank == '6':

            total += 6

        elif rank == '7':

            total += 7

        elif rank == '8':

            total += 8

        elif rank == '9':

            total += 9

    return total


print ("Your current hand is {}".format(user_hand))

print ("This provides you with a total of:")

print (handtotal(user_hand))
Navneet
  • 4,543
  • 1
  • 19
  • 29
  • Okay, I get the calling the index [1] to specify only the card value itself. The code above for me is currently returning only the first value of the 2 cards in the hand. I thought the for loop went through each item in the list (or tuple). – mtomney Aug 19 '15 at 16:23
-2

I think that the problem is with this:

if rank == "J" or "Q" or "K":

I know you can do a lot of weird things with python but I'm pretty sure the statement should be

if rank=="J" or rank=="Q" or rank=="K"...

Renee
  • 17
  • 3