Processing: Blackjack.ipynb…
can somebody fix this code line 40 pls
import random
playerIn = True
dealerIn = True
deck of cards / player dealer hand
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10,
‘J’, ‘Q’, ‘K’, ‘A’ ‘J’, ‘Q’, ‘K’, ‘A’ ‘J’, ‘Q’, ‘K’, ‘A’ ‘J’, ‘Q’, ‘K’, ‘A’]
deal the cards
def dealCard(turn):
card = random.choice(deck)
turn.append(card)
deck.remove(card)
calculate the total of each hand
def total(turn):
total = 0
face = [‘J’, ‘K’, ‘Q’]
for card in turn:
if card in range(1, 11):
total += card
elif card in face:
total +=10
else:
if total > 11:
total += 1
else:
total += 11
return total
check for winner
def revealDealerHand():
if len(dealerHand) == 2:
return dealerHand[0]
elif len(dealerHand) > 2:
return dealerHand[0], dealerHand[1]
game loop
for _ in range(2):
dealCard(dealerHand)
dealCard(playerHand)
while playerIn or dealerIn:
print(f"Dealer had {revealDealerHand()} and X")
print(f"You Have {playerHand} for a total of {total(playerHand)}")
if playerIn:
stayOrHit = input(“1: Stay\n2: Hit\n”)
if total(dealerHand) > 16:
dealerIn = False
else:
dealCard(dealerHand)
if stayOrHit == ‘1’:
playerIn = False
else:
dealCard(dealerHand)
if total(playerHand) >= 21:
break
elif total(dealerHand) >=21:
break
if total(playerHand) == 21:
print(f"\nYou have {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print(“Blackjack! You Win!”)
elif total(dealerHand) == 21:
print(f"\nYou have {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print(“Blackjack! Dealer Wins!”)
elif total(playerHand) > 21:
print(f"\nYou have {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print(“You Bust! Dealer Wins”)
elif total(dealerHand) > 21:
print(f"\nYou have {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print(“Dealer Busts! You Win”)
elif 21 - total(dealerHand) > 21 - total(playerHand):
print(f"\nYou have {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print(“Dealer WIns!”)
elif 21 - total(dealerHand) < 21 - total(playerHand):
print(f"\nYou have {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print(“You Win!”)
By my count, line 40 is a blank line. Maybe I miscounted.
Do you think it is fair on us for you to ask us to count the lines by
hand to work out what the error is? You have the full error message
shown to you. Please copy and paste the error message, starting with the
line “Traceback…” and going to the end.
But my guess is that the error comes from this line:
dealCard(dealerHand)
just inside the game loop:
for _ in range(2):
dealCard(dealerHand)
dealCard(playerHand)
I don’t think you have defined dealerHand or playerHand yet. I think you
need these two lines just before the game loop:
dealerHand = [] # initialise with an empty list
playerHand = []