-2

I'm currently working on a blackjack project, and just started, but when I print the randomly generated card from the cards list, it duplicates the cards and prints 4 when I coded it to print 2??

(hand_val = value of cards in player hand, hand = player hand)

import random as r
from src.player.player import Player as p

print("--------------------\nWelcome to Blackjack\n--------------------")


def handcalc():
    cards = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
    select = r.choice(cards)
    if select == "1":
        p.hand_val.append(1)
        p.hand.append(select)
    if select == "2":
        p.hand_val.append(2)
        p.hand.append(select)
    if select == "3":
        p.hand_val.append(3)
        p.hand.append(select)
    if select == "4":
        p.hand_val.append(4)
        p.hand.append(select)
    if select == "5":
        p.hand_val.append(5)
        p.hand.append(select)
    if select == "6":
        p.hand_val.append(6)
        p.hand.append(select)
    if select == "7":
        p.hand_val.append(7)
        p.hand.append(select)
    if select == "8":
        p.hand_val.append(8)
        p.hand.append(select)
    if select == "9":
        p.hand_val.append(9)
        p.hand.append(select)
    if select == "10" or "J" or "Q" or "K":
        p.hand_val.append(10)
        p.hand.append(select)


def blackjack():
    for i in range(2):
        handcalc()
    print(p.hand)


blackjack()
keqae
  • 15
  • 3
  • 1
    change `if select == "10" or "J" or "Q" or "K":` for `if select in ("10", "J", "Q", "K"):`. –  Aug 29 '22 at 11:46
  • 1
    Does this answer your question? [Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al) – matszwecja Aug 29 '22 at 11:50
  • Please include the Player class, your code cannot be run currently. There could be another error there. If there isn't this is a duplicate of [Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true-how-can-i-compare-a-to-al) because of the line `if select == "10" or "J" or "Q" or "K":`. – Lomtrur Aug 29 '22 at 11:50
  • Please also see [mcve]. In your case it may be useful to not have random values but specific cases and then describing which cases are wrong with expected output. For example your output most likely has the length 2 if J, K or Q are randomly selected twice, and length 3 if they are selected once. Expected input (instead of random) and expected output versus actual (wrong) output are helpful when answering your question. – Lomtrur Aug 29 '22 at 11:54
  • Thanks, I haven't worked on data structures a lot and didn't realize that other data structures would be more suitable for this kind of thing. Since it's difficult to get the individual key and value for a dictionary I added tuples for each card. – keqae Aug 29 '22 at 11:55

2 Answers2

2

I guess your problem is here (output result could be useful):

if select == "10" or "J" or "Q" or "K":
    p.hand_val.append(10)
    p.hand.append(select)

The or operator is used to combine several conditions.
That is, select == "10", "J", "Q", "K", are separate conditions and "J", "Q", "K" are all evaluated to True.
In such case, it doesn't matter what is in select, this if statement will evaluate to True and result with an extra card for each card (other than "10", "J", "Q", "K").
So just replace it with:

if select == "10" or select == "J" or select == "Q" or select == "K":
    ...

Or

if select in ["10", "J", "Q", "K"]:
    ...

Note also that this happens in addition to another card since you are using if statement, while using elif from the second statement onwards would cause in evaluating statements just until one of them is True. Although this will be irrelevant once you solve the above if statement.

-2

Your issue is likely coming from the fact that you have called handcalc twice

for i in range(2):
    handcalc()

so get rid of for i in range(2): and unindent your code. but, we need to see your player class to be sure.


Also

please use tuples to assign values to your cards

    cards = [("1", 1), ("2", 2), ("3", 3), ("4", 4), ("5", 5), ("6", 6), ("7", 7), ("8", 8), ("9", 9), ("10", 10), ("J", 10), ("Q", 10), ("K", 10)]

So to put all of this into fruition, your code should look like this:

import random as r
from src.player.player import Player as p

print("--------------------\nWelcome to Blackjack\n--------------------")


def handcalc():
    cards = [("1", 1), ("2", 2), ("3", 3), ("4", 4), ("5", 5), ("6", 6), ("7", 7), ("8", 8), ("9", 9), ("10", 10), ("J", 10), ("Q", 10), ("K", 10)]
    select = r.choice(cards)
    p.hand_val.append(select[1])
    p.hand.append(select[0]


def blackjack():
    handcalc()
    print(p.hand)


blackjack()
aw1lt
  • 24
  • 1
  • 4