显示一个字母并将其用于 Blackjack 中的数学运算
Showing a Letter and Using it for a Mathematical Operation In Blackjack
我正在做一个 Blackjack 项目。
我希望能够在整数列表 2-10 中使用字母“A”。
我迷失了传递字符串值的过程,在本例中是 'A' 到卡片的 sum() 函数中,然后 returning 该值但显示 'A' 在卡片中我迷路了。
例如:
卡片 = ['A',2,3,4,5,6,7,8,9,10]
该列表将随机 return 2 个值,范围从 A-10
然后如果 A 是 returned 与另一个数字我想计算字母 A + 整数的值
它会打印给用户类似
“你的牌是:[A,7] 总 = 18”
这是我到目前为止一直在尝试的。
import random
A = ord("A") - 64
def deal():
cards = [A, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player = []
dealer = []
while len(player) < 2:
player.append(random.choice(cards))
dealer.append(random.choice(cards))
return player, dealer
#this is ITERABLE UNPACK it separates the returned values from the deal() function and assigns them to their respective variables.
player, dealer = deal()
#sum the value of the cards
player_cards = sum(player)
dealer_cards = sum(dealer)
print(f"Your cards are {player}. Total = {player_cards}")
print(f"The dealer shows [{dealer[0]}, *]. Total = {dealer_cards}")
尝试:
def get_sum(cards):
return sum(card if not card == 'A' else 11 for card in cards)
player = ['A', 7]
dealer = [8, 'A']
player_cards = get_sum(player)
dealer_cards = get_sum(dealer)
print(f"Your cards are {', '.join([', '.join(str(i) for i in player)])}. Total = {player_cards}")
print(f"The dealer shows {dealer[0]}. Total = {dealer_cards}")
输出:
Your cards are A, 7. Total = 18
The dealer shows 8. Total = 19
如果你有King、Queen和Jack,你可以使用这个版本:
player = ['A', 'K']
def get_sum(cards):
vals = {'A': 11, 'K': 10, 'Q': 10, 'J': 10}
return sum(vals.get(card, card) for card in cards)
print(get_sum(player))
print(f"Your cards are {player}. Total = {player_cards}")
print(f"The dealer shows [{dealer[0]}, *]. Total = {dealer_cards}")
输出:
21
在处理卡片时,我更喜欢使用random.sample(cards, len(cards))
,因为这个函数模拟卡片的混合。
# let's define cards
card_values = {'A': 11, 'K': 10, 'Q': 10, 'J': 10}
card_values.update({k: k for k in range(2, 11)})
# because I am too lazy to write all card values by hand
cards = card_values.keys()
def random_select(cards, k=2):
"""Select randomly k cards from cards.
Return also the not-selected cards."""
new_order = random.sample(cards, len(cards))
return new_order[:k], new_order[k:]
# and you can choose first 2 by:
two_cards, rest_deck = random_select(cards, k=2)
# by altering k you can make the function select k cards.
# and for next player you do:
two_next_cards, rest_deck = random_select(rest_deck, k=2)
# in this way, the already selected cards are removed from the original deck (rest_deck).
def card_sums(cards, card2value=card_values):
return sum([card2value[c] for c in cards])
def deal(players, cards, k=2):
result = {}
rest_cards = cards
for player in players:
chosen, rest_cards = random_select(rest_cards, k = k)
result[player] = chosen
return result, rest_cards
# players can be a dictionary
# or just a list of the players (strings).
players = {'player': [], 'dealer': []}
selected_cards, rest_cards = deal(players, cards, k=2)
print(f"Your cards are {selected_cards['player']}. Total = {card_sums(selected_cards['player'])}")
print(f"The dealer shows {selected_cards['dealer']}. Total = {card_sums(selected_cards['dealer'])}")
我正在做一个 Blackjack 项目。
我希望能够在整数列表 2-10 中使用字母“A”。
我迷失了传递字符串值的过程,在本例中是 'A' 到卡片的 sum() 函数中,然后 returning 该值但显示 'A' 在卡片中我迷路了。
例如: 卡片 = ['A',2,3,4,5,6,7,8,9,10]
该列表将随机 return 2 个值,范围从 A-10 然后如果 A 是 returned 与另一个数字我想计算字母 A + 整数的值
它会打印给用户类似 “你的牌是:[A,7] 总 = 18”
这是我到目前为止一直在尝试的。
import random
A = ord("A") - 64
def deal():
cards = [A, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player = []
dealer = []
while len(player) < 2:
player.append(random.choice(cards))
dealer.append(random.choice(cards))
return player, dealer
#this is ITERABLE UNPACK it separates the returned values from the deal() function and assigns them to their respective variables.
player, dealer = deal()
#sum the value of the cards
player_cards = sum(player)
dealer_cards = sum(dealer)
print(f"Your cards are {player}. Total = {player_cards}")
print(f"The dealer shows [{dealer[0]}, *]. Total = {dealer_cards}")
尝试:
def get_sum(cards):
return sum(card if not card == 'A' else 11 for card in cards)
player = ['A', 7]
dealer = [8, 'A']
player_cards = get_sum(player)
dealer_cards = get_sum(dealer)
print(f"Your cards are {', '.join([', '.join(str(i) for i in player)])}. Total = {player_cards}")
print(f"The dealer shows {dealer[0]}. Total = {dealer_cards}")
输出:
Your cards are A, 7. Total = 18
The dealer shows 8. Total = 19
如果你有King、Queen和Jack,你可以使用这个版本:
player = ['A', 'K']
def get_sum(cards):
vals = {'A': 11, 'K': 10, 'Q': 10, 'J': 10}
return sum(vals.get(card, card) for card in cards)
print(get_sum(player))
print(f"Your cards are {player}. Total = {player_cards}")
print(f"The dealer shows [{dealer[0]}, *]. Total = {dealer_cards}")
输出:
21
在处理卡片时,我更喜欢使用random.sample(cards, len(cards))
,因为这个函数模拟卡片的混合。
# let's define cards
card_values = {'A': 11, 'K': 10, 'Q': 10, 'J': 10}
card_values.update({k: k for k in range(2, 11)})
# because I am too lazy to write all card values by hand
cards = card_values.keys()
def random_select(cards, k=2):
"""Select randomly k cards from cards.
Return also the not-selected cards."""
new_order = random.sample(cards, len(cards))
return new_order[:k], new_order[k:]
# and you can choose first 2 by:
two_cards, rest_deck = random_select(cards, k=2)
# by altering k you can make the function select k cards.
# and for next player you do:
two_next_cards, rest_deck = random_select(rest_deck, k=2)
# in this way, the already selected cards are removed from the original deck (rest_deck).
def card_sums(cards, card2value=card_values):
return sum([card2value[c] for c in cards])
def deal(players, cards, k=2):
result = {}
rest_cards = cards
for player in players:
chosen, rest_cards = random_select(rest_cards, k = k)
result[player] = chosen
return result, rest_cards
# players can be a dictionary
# or just a list of the players (strings).
players = {'player': [], 'dealer': []}
selected_cards, rest_cards = deal(players, cards, k=2)
print(f"Your cards are {selected_cards['player']}. Total = {card_sums(selected_cards['player'])}")
print(f"The dealer shows {selected_cards['dealer']}. Total = {card_sums(selected_cards['dealer'])}")