Python Forum

Full Version: The program isn't counting the total properly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#blackjack

import random
deck = ["2+diamonds","3+diamonds","4+diamonds","5+diamonds","6+diamonds","7+diamonds","8+diamonds","9+diamonds","10+diamonds","j+diamonds","q+diamonds","k+diamonds","a+diamonds","2+clubs","3+clubs","4+clubs","5+clubs","6+clubs","7+clubs","8+clubs","9+clubs","10+clubs","j+clubs","q+clubs","k+clubs","a+clubs","2+hearts","3+hearts","4+hearts","5+hearts","6+hearts","7+hearts","8+hearts","9+hearts","10+hearts","j+hearts","q+hearts","k+hearts","a+hearts","2+spades","3+spades","4+spades","5+spades","6+spades","7+spades","8+spades","9+spades","10+spades","j+spades","q+spades","k+spades","a+spades"]
playershand = []
dealerhand = []

for i in range (2):
    a = (random.choice(deck))
    print(a)
    deck.remove(a)
    playershand.append(a)
    

firstcard = playershand[0]
secondcard = playershand[1]
x = firstcard.split("+")
y = secondcard.split("+")

num1 = x[0]
num2 = y[0]

if num1 == ("2") or num1 == ("3") or num1 == ("4") or num1 == ("5") or num1 == ("6") or num1 == ("7") or num1 == ("8") or num1 == ("9") or num1 == ("10"):
    num1 = int(num1)
if num1 == ("j") or num1 == ("q") or num1 == ("k"):
    num1 = (10)
    num1 = int(num1)
if num1 == ("a"):
    num1 = (11)
    num1 = int(num1)
if num2 == ("2") or num2 == ("3") or num2 == ("4") or num2 == ("5") or num2 == ("6") or num2 == ("7") or num2 == ("8") or num2 == ("9") or num2 == ("10"):
    num2 = int(num2)
if num2 == ("j") or num2 == ("q") or num2 == ("k"):
    num2 = (10)
    num2 = int(num2)
if num2 == ("a"):
    num2 = (11)
    num2 = int(num2)

total = (num1 + num2)
print("The total is",total)

b = (random.choice(deck))
deck.remove(b)
c = (random.choice(deck))
deck.remove(c)
#to edit
print("The dealer has", b,c)

b = b.split("+")
c = c.split("+")

dnum1 = b[0]
dnum2 = c[0]

if dnum1 == ("2") or dnum1 == ("3") or dnum1 == ("4") or dnum1 == ("5") or dnum1 == ("6") or dnum1 == ("7") or dnum1 == ("8") or dnum1 == ("9") or dnum1 == ("10"):
    dnum1 = int(dnum1)
if dnum1 == ("j") or dnum1 == ("q") or dnum1 == ("k"):
    dnum1 = (10)
    dnum1 = int(dnum1)
if dnum1 == ("a"):
    dnum1 = (11)
    dnum1 = int(dnum1)
if dnum2 == ("2") or dnum2 == ("3") or dnum2 == ("4") or dnum2 == ("5") or dnum2 == ("6") or dnum2 == ("7") or dnum2 == ("8") or dnum2 == ("9") or dnum2 == ("10"):
    num2 = int(num2)
if dnum2 == ("j") or dnum2 == ("q") or dnum2 == ("k"):
    dnum2 = (10)
    dnum2 = int(dnum2)
if dnum2 == ("a"):
    dnum2 = (11)
    dnum2 = int(dnum2)

dnum1 = int(dnum1)
dnum2 = int(dnum2)
dtotal = dnum1 + dnum2

ans = input("Would you like another card? (y/n) ")
while ans == "y":
    xcard = (random.choice(deck))
    print(xcard)
    playershand.append(xcard)
    deck.remove(xcard)
    print (playershand)
    x = xcard.split("+")
    xnum = x[0]
    if xnum == ("2") or xnum == ("3") or xnum == ("4") or xnum == ("5") or xnum == ("6") or xnum == ("7") or xnum == ("8") or xnum == ("9") or xnum == ("10"):
        xnum = int(xnum)
    elif xnum == ("j") or xnum == ("q") or xnum == ("k"):
        xnum = (10)
        xnum = int(xnum)
    elif xnum == ("a"):
        xnum = (11)
        xnum = int(xnum)
    total = num1 + num2 + xnum
    print(total)
    if total < 21:
        ans = input("Would you like another card? (y/n) ")
        continue
    if total > 21:
        print("BUST, you lose!")
        break
    if total == 21:
        print("BLACKJACK, you win!")
        break
The program counts the first extra card correctly but if you want another one it doesn't count it correctly
It hurts my eyes reading your code, but here you have it:

Line 82: total = num1 + num2 - as a sum of cards from previous draws
Line 99: total = total + xnum - increment total each time you draw new card

# blackjack

import random

deck = ["2+diamonds", "3+diamonds", "4+diamonds", "5+diamonds", "6+diamonds", "7+diamonds", "8+diamonds", "9+diamonds",
        "10+diamonds", "j+diamonds", "q+diamonds", "k+diamonds", "a+diamonds", "2+clubs", "3+clubs", "4+clubs",
        "5+clubs", "6+clubs", "7+clubs", "8+clubs", "9+clubs", "10+clubs", "j+clubs", "q+clubs", "k+clubs", "a+clubs",
        "2+hearts", "3+hearts", "4+hearts", "5+hearts", "6+hearts", "7+hearts", "8+hearts", "9+hearts", "10+hearts",
        "j+hearts", "q+hearts", "k+hearts", "a+hearts", "2+spades", "3+spades", "4+spades", "5+spades", "6+spades",
        "7+spades", "8+spades", "9+spades", "10+spades", "j+spades", "q+spades", "k+spades", "a+spades"]

BASIC_DECK = ['2', '3', '4', '5', '6', '7', '8', '9', '10']
JQK_DECK = ['j', 'q', 'k']
ACE_DECK = ['a']

playershand = []
dealerhand = []

for i in range(2):
    a = random.choice(deck)
    print(a)
    deck.remove(a)
    playershand.append(a)

firstcard = playershand[0]
secondcard = playershand[1]
x = firstcard.split("+")
y = secondcard.split("+")

num1 = x[0]
num2 = y[0]

if num1 in BASIC_DECK:
    num1 = int(num1)
elif num1 in JQK_DECK:
    num1 = 10
elif num1 in ACE_DECK:
    num1 = 11

if num2 in BASIC_DECK:
    num2 = int(num2)
elif num2 in JQK_DECK:
    num2 = 10
elif num2 in ACE_DECK:
    num2 = 11

total = num1 + num2
print("The total is", total)

b = random.choice(deck)
deck.remove(b)
c = random.choice(deck)
deck.remove(c)

print("The dealer has", b, c)

b = b.split("+")
c = c.split("+")

dnum1 = b[0]
dnum2 = c[0]

if dnum1 in BASIC_DECK:
    dnum1 = int(dnum1)
elif dnum1 in JQK_DECK:
    dnum1 = 10
elif dnum1 in ACE_DECK:
    dnum1 = 11

if dnum2 in BASIC_DECK:
    dnum2 = int(dnum2)
elif dnum2 in JQK_DECK:
    dnum2 = 10
elif dnum2 in ACE_DECK:
    dnum2 = 11

dnum1 = int(dnum1)
dnum2 = int(dnum2)
dtotal = dnum1 + dnum2

ans = input("Would you like another card? (y/n) ")
total = num1 + num2
while ans == "y":
    xcard = (random.choice(deck))
    print(xcard)
    playershand.append(xcard)
    deck.remove(xcard)
    print(playershand)
    x = xcard.split("+")
    xnum = x[0]

    if xnum in BASIC_DECK:
        xnum = int(xnum)
    elif xnum in JQK_DECK:
        xnum = 10
    elif xnum in ACE_DECK:
        xnum = 11

    total = total + xnum
    print(total)
    if total < 21:
        ans = input("Would you like another card? (y/n) ")
        continue
    if total > 21:
        print("BUST, you lose!")
        break
    if total == 21 or total>dtotal:
        print("BLACKJACK, you win!")
        break
EDIT: Line 107 if total == 21 or total>dtotal: - at blackjack you win when you have greater cards than dealer