Python duplicates a value in a list and I have no idea why

Question:

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 Blackjackn--------------------")


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()
Asked By: keqae

||

Answers:

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 Blackjackn--------------------")


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()
Answered By: Addikted Montage

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.

Answered By: Etay Schwartzberg
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.