[英]How do I replace the values of a key in Python?
I have this Blackjack game where I am trying to make changes to the function calculate_hand_value (hand)
and take into account that an Ace can have the value 1 instead of 11 (if the total value is over 21, you want the Ace to count as 1).我有这个二十一点游戏,我试图对函数
calculate_hand_value (hand)
进行更改,并考虑到 Ace 的值可以为 1 而不是 11(如果总值超过 21,您希望 Ace 算作1)。 Can anyone help me?谁能帮我?
import random
full_deck = {"Two of clubs": 2, "Three of clubs": 3, "Four of clubs": 4, "Five of clubs": 5, "Six of clubs": 6,
"Seven of clubs": 7, "Eight of clubs": 8, "Nine of clubs": 9, "Ten of clubs": 10,
"Jack of clubs": 10, "Queen of clubs": 10, "King of clubs": 10, "Ace of clubs": 11,
"Two of diamonds": 2, "Three of diamonds": 3, "Four of diamonds": 4, "Five of diamonds": 5,
"Six of diamonds": 6, "Seven of diamonds": 7, "Eight of diamonds": 8, "Nine of diamonds": 9,
"Ten of diamonds": 10, "Jack of diamonds": 10, "Queen of diamonds": 10, "King of diamonds": 10,
"Ace of diamonds": 11, "Two of hearts": 2, "Three of hearts": 3, "Four of hearts": 4, "Five of hearts": 5,
"Six of hearts": 6,"Seven of hearts": 7, "Eight of hearts": 8, "Nine of hearts": 9, "Ten of hearts": 10,
"Jack of hearts": 10, "Queen of hearts": 10, "King of hearts": 10, "Ace of hearts": 11,
"Two of spades": 2, "Three of spades": 3, "Four of spades": 4, "Five of spades": 5, "Six of spades": 6,
"Seven of spades": 7, "Eight of spades": 8, "Nine of spades": 9, "Ten of spades": 10,
"Jack of spades": 10, "Queen of spades": 10, "King of spades": 10, "Ace of spades": 11,
}
def get_new_shuffled_deck():
deck = list(full_deck.keys())
random.shuffle(deck)
return deck
def get_card_value(card):
return full_deck[card]
def calculate_hand_value(hand):
hand_value = 0
for card in hand:
hand_value += get_card_value(card)
return hand_value
for card in hand:
hand_value += get_card_value(card)
if hand_value > 21:
hand_value -= 10
return hand_value
you could add a counter for aces and in case of being over 21, you could subtract 10 points for each ace.您可以为 A 添加一个计数器,如果超过 21,您可以为每个 A 减去 10 分。
def calculate_hand_value(hand):
hand_value = 0
aces = 0
for card in hand:
card_value = get_card_value(card)
if card_value = 11:
aces += 1
hand_value += card_value
for _ in range(aces):
if hand_value > 21:
hand_value -= 10
return hand_value
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.