我正在嘗試做一個小二十一點遊戲,並且在一個單獨的文件中我已經調用了playing_cards.py,它包含具有以下內容的「deck」。我的問題是,如何在不需要大量if語句(仍然導致問題)的情況下執行所有這些操作並仍然生成正確的數字。我還是一個初學者,所以任何幫助將是美妙的Python Blackjack Game
deck = ['AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS',
'AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC']
import playing_cards
player_hand = []
card = playing_cards.deal_one_card()
player_hand.append(card)
card = playing_cards.deal_one_card()
player_hand.append(card)
dealer_hand = []
card = playing_cards.deal_one_card()
dealer_hand.append(card)
card = playing_cards.deal_one_card()
dealer_hand.append(card)
print(dealer_hand)
if player_hand[0][0] == "A":
player_hand[0] = 11
print(player_hand)
elif player_hand[1][0] == "A":
player_hand[1] = 11
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "2":
player_hand[0] = 2
print(player_hand)
elif player_hand[1][0] == "2":
player_hand[1] = 2
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "3":
player_hand[0] = 3
print(player_hand)
elif player_hand[1][0] == "3":
player_hand[1] = 3
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "4":
player_hand[0] = 4
print(player_hand)
elif player_hand[1][0] == "4":
player_hand[1] = 4
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "5":
player_hand[0] = 5
print(player_hand)
elif player_hand[1][0] == "5":
player_hand[1] = 5
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "6":
player_hand[0] = 6
print(player_hand)
elif player_hand[1][0] == "6":
player_hand[1] = 6
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "7":
player_hand[0] = 7
print(player_hand)
elif player_hand[1][0] == "7":
player_hand[1] = 7
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "8":
player_hand[0] = 8
print(player_hand)
elif player_hand[1][0] == "8":
player_hand[1] = 8
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "9":
player_hand[0] = 9
print(player_hand)
elif player_hand[1][0] == "9":
player_hand[1] = 9
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "T":
player_hand[0] = 10
print(player_hand)
elif player_hand[1][0] == "T":
player_hand[1] = 10
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "J":
player_hand[0] = 10
print(player_hand)
elif player_hand[1][0] == "J":
player_hand[1] = 10
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "Q":
player_hand[0] = 10
print(player_hand)
elif player_hand[1][0] == "Q":
player_hand[1] = 10
print(player_hand)
else:
print(player_hand)
if player_hand[0][0] == "K":
player_hand[0] = 10
print(player_hand)
elif player_hand[1][0] == "K":
player_hand[1] = 10
print(player_hand)
else:
print(player_hand)
您可以定義一個字典,在該字典中定義'card to result mapping relation',然後可以調用'compare/assign/print function'一次。 – nick