【问题标题】:Updating variable in for loop在 for 循环中更新变量
【发布时间】:2021-12-17 07:54:25
【问题描述】:

正在做一个家庭作业,我必须编写三个函数来创建一个简单的二十一点游戏。

  • 第一个函数card_to_value 接收一个表示卡片的 str,该函数返回每张卡片的 int 值,“A”仅返回 11。
  • 第二个函数计算给定手牌的硬分,其中“A”为 11,无论手牌中有多少 A。
  • 第三个函数计算软分数,其中第一个“A” 手中是 11,但所有后续的 A 都值 1,例如 手“AAA”应该在软评分函数中返回 13,但在 硬分功能。

我的代码目前适用于前两个 功能,但不会为软分数返回正确的值 功能。

def card_to_value(card=''):
    card_list=['2','3','4','5','6','7','8','9','T','J','Q','K','A']
        while card in list(card_list):
           if card in card_list[8:12]:
            card=10
            return card
           if card in card_list[12:]:
            card=1
            return card
           if card == '2':
            card=2
            return card
           if card == '3':
            card=3
            return card
           if card == '4':
            card=4
            return card
           if card == '5':
            card=5
            return card
           if card == '6':
            card=6
            return card
           if card == '7':
            card=7
            return card
           if card == '8':
            card=8
            return card
           if card == '9':
            card=9
            return card

def hard_score(hand):
    h = list(hand)
    total=0
    for each in h:
        total=total+card_to_value(each)
    return total

def soft_score(hand):
    ace_found=False
    h = list(hand)
    total = 0
    for each in h:
        total = total + card_to_value(each)
        if each == 'A':
            ace_found=True
        elif ace_found == True:
            total += 1
        else: total += 11

return total

【问题讨论】:

  • 拿纸和铅笔,写下您的 soft_score 将计算的值。你会很快看到你的问题。
  • 或者(甚至更好)学习如何使用调试器。你可以从这里开始:jetbrains.com/help/pycharm/part-1-debugging-python-code.html
  • card_to_value 中不需要while 循环。
  • 而且没有必要在列表中调用listreturn card 不应该出现在每个 if 语句中,而应该出现在这个级联的末尾。第一个之后的每个ifs 都应该是elif。当然,如果大多数值都是硬编码的,那么使用列表的方法很奇怪。

标签: python for-loop boolean blackjack


【解决方案1】:

您添加到total 两次。在调用card_to_value之前,您需要检查它是否是 Ace。

def soft_score(hand):
    ace_found=False
    h = list(hand)
    total = 0
    for each in h:
        total = total + card_to_value(each)
        # handle the aces first 
        if each == 'A':
          if not ace_found :
            ace_found=True
            total += 11
          else :
            total += 1
        # handle all other cards
        else: 
            total += card_to_value(each)

【讨论】:

    【解决方案2】:

    也许这样的东西对你有用……

    import itertools
    
    # Dictionary of possible card values
    card_values = {
        '2':[2],
        '3':[3],
        '4':[4],
        '5':[5],
        '6':[6],
        '7':[7],
        '8':[8],
        '9':[9],
        'T':[10],
        'J':[10],
        'Q':[10],
        'K':[10],
        'A':[11,1],
    }
    
    # Function that takes a list of cards considered a "hand"
    # and returns all possible hand totals
    def get_possible_totals(hand):
        values=[card_values.get(card) for card in hand]
        return [sum(tup) for tup in itertools.product(*values)]
    

    示例用法:

    hand = ['5','A','A']
    get_possible_totals(hand)
    
    returns:
    [27, 17, 17, 7]
    
    

    【讨论】:

      【解决方案3】:

      我更改了所有函数以显示它是如何编写的。对于初学者来说,这可能是太多的输入,但是如果您尝试并设法理解它(在几天、几周或几个月内),您就会学到很多东西。

      card_to_value 函数使用字典来获取卡片的正确值。在这个函数中使用print(card_values),看看构造出来的字典是什么样子的。

      hard_score 显示了 sum 与值生成器的用法。

      soft_score 使用原始方法。但是由于card_to_value 现在返回11 获得一张A,我们从第一个A 之后的每个A 的总数中减去10。

      def card_to_value(card=''):
          card_values = dict(zip('23456789TJQKA', [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]))
          return card_values.get(card, 0)
      
      
      def hard_score(hand):
          return sum(card_to_value(card) for card in hand)
      
      
      def soft_score(hand):
          ace_found = False
          total = 0
          for card in hand:
              total += card_to_value(card)
              if card == 'A':
                  if not ace_found:
                      ace_found = True
                  else:
                      total -= 10  # subtract 10 if it is not the first ace
          return total
      
      
      hand = ['3', 'A', '5', 'A']
      print(hard_score(hand))
      print(soft_score(hand))
      

      这将打印出3020


      soft_score的奖励版:

      def soft_score(hand):
          return hard_score(hand) - max(hand.count('A') - 1, 0) * 10
      

      想法:数一数手中的ace数,减1,留下最小值0,再乘以10,然后从硬分中减去。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-20
        • 1970-01-01
        • 2016-09-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多