import random
deckOfCards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
playerHand = []
computerHand = []
def testWin():
if sum(playerHand) == sum(computerHand):
print("Draw")
elif sum(playerHand) == 21:
print("Blackjack! You win")
elif sum(computerHand) == 21:
print("Computer has blackjack you lose")
if sum(playerHand) > 21:
if sum(computerHand) < 21:
print("You lost")
elif sum(computerHand) > 21:
print("Draw")
elif sum(computerHand) > 21:
if sum(playerHand) < 21:
print("You win")
elif sum(playerHand) > 21:
print("Draw")
elif sum(playerHand) < 21:
if sum(computerHand) > 21:
print("You win!")
elif sum(computerHand) < 21 and sum(computerHand) < sum(playerHand):
print("You win")
elif sum(computerHand) < 21 and sum(computerHand) > sum(computerHand):
print("You lose")
def drawPlayerCard():
playerHand.append(deckOfCards[random.randint(0, 9)])
print("Your Cards are:", playerHand)
print("total:", sum(playerHand), "\n")
if len(playerHand) < 2:
drawPlayerCard()
drawComputerHand()
def drawComputerHand():
if sum(computerHand) <= 17:
computerHand.append(deckOfCards[random.randint(0, 9)])
print("the computer has:", computerHand)
print("total:", sum(computerHand), "\n")
if len(computerHand) < 2:
drawComputerHand()
hitStand()
else:
print("the computer stands with a total of:", sum(computerHand))
hitStand()
def hitStand():
option = input("do you want to hit or stand? [h/s]")
if option.lower() == "h":
drawPlayerCard()
elif option.lower() == "s":
testWin()
else:
print("please say if you want to hit or stand!")
hitStand()
def start():
startGaming = input("Do you want to play Blackjack? [y/n]")
if startGaming == "y":
drawPlayerCard()
elif startGaming == "n":
pass
else:
print("please state if you want to start the game")
start()
start()
Hey, i'm kinda new to pyhton and i tried to create a simple blackjack game. it doesn't entirely work as intended. when i do stand i get an never ending loop of "do you want to hit or stand? [h/s]" or things like this
do you want to hit or stand? [h/s]h
Your Cards are: [10, 2, 4]
total: 16
the computer has: [9, 6, 4]
total: 19
do you want to hit or stand? [h/s]s
Your Cards are: [10, 2, 4, 3]
total: 19
Draw
do you want to hit or stand? [h/s]s
Your Cards are: [10, 2, 4, 3, 4]
total: 23
You lost
the computer stands with a total of: 19
do you want to hit or stand? [h/s]s
Your Cards are: [10, 2, 4, 3, 4, 2]
total: 25
You lost
before the code stops and i cant figure out why it does this. it gives a loop of do you want to hit or stand, until it suddenly stops and suddenly adds another card without me hitting
The problem is that "drawPlayerCard()" calls "drawComputerHand()" and "drawComputerHand()" calls "hitStand()".
Explanation:
first problem in "drawPlayerHand()":
if len(playerHand) < 2:
drawPlayerCard()
drawComputerHand()
When your if statement is true, "drawPlayerHand()" is executed a second time. This means that "drawComputerHand()" is also executed 2 times.
And you have the same problem in "drawComputerHand()":
if len(computerHand) < 2:
drawComputerHand()
hitStand()
Try to fix it yourself. If you arent able to fix it, I can post some more code.