import random
playerIn = True
dealerIn = True
#deck/player 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']
playerHand = []
dealerHand = []
#deal cards
def dealCard(turn):
card = random.choice(deck)
turn.append(card)
turn.remove(card)
#calculate hand total
def total(turn):
total = 0
face = ['Q', 'K', 'J']
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"The 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(playerHand)
if total(playerHand) >= 21:
break
elif total(dealerHand) >= 21:
break
if total(playerHand) == 21:
print(f"\nYou have {playerHand} for a total of {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 {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 {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 {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 {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 {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("You win.")
Когда программа достигает строки 47, вместо того, чтобы получить значения двух карт и общую сумму, я получаю:
У дилера была и X
У вас есть [] на общую сумму 0.
Я долго пытался выяснить, как заставить распечатывать руки игрока и дилера, а общее количество игроков - распечатайте, но не могу понять этого хоть убей. Есть предложения?
Я подумал, что мне нужно преобразовать значения рук игрока и дилера в целое число, но это не сработало. Возможно, я упускаю что-то действительно очевидное, но, честно говоря, понятия не имею.
#calculate hand total def total(turn): total = 0 face = ['Q', 'K', 'J'] 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"The 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(playerHand) if total(playerHand) >= 21: break elif total(dealerHand) >= 21: break
if total(playerHand) == 21: print(f"\nYou have {playerHand} for a total of {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 {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 {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 {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 {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 {playerHand} and the dealer has {dealerHand} for a total of {total(dealerHand)}") print("You win.") [/code] Когда программа достигает строки 47, вместо того, чтобы получить значения двух карт и общую сумму, я получаю: У дилера была и X У вас есть [] на общую сумму 0. Я долго пытался выяснить, как заставить распечатывать руки игрока и дилера, а общее количество игроков - распечатайте, но не могу понять этого хоть убей. Есть предложения? Я подумал, что мне нужно преобразовать значения рук игрока и дилера в целое число, но это не сработало. Возможно, я упускаю что-то действительно очевидное, но, честно говоря, понятия не имею.