【问题标题】:Python blackjack: How do I structure an if statement to determine whether an Ace should be turned into 1?Python blackjack:如何构造一个 if 语句来确定 Ace 是否应该变成 1?
【发布时间】:2020-02-02 07:18:57
【问题描述】:

我正在尝试解决一个简单的编码练习:给定 1 到 11 之间的三个整数,如果它们的总和小于或等于 21,则返回它们的总和。如果它们的总和超过 21 并且有 11,则减少 11。最后,如果总和(即使经过调整)超过 21,则返回 'BUST'

def blackjack(a,b,c):
    for i in range(0,12):
        if sum((a,b,c)) <= 21:
            return sum((a,b,c))
        elif sum ((a,b,c)) > 21 and 11 in (a,b,c):
            if a == 11:
                a -= 10
                continue
                if b == 11 and sum((a,b,c)) > 21:
                    b -= 10
                    continue
                    if c == 11 and sum((a,b,c)) > 21:
                        c -=10
                        break
                        return sum((a,b,c))

        else:
            return 'Bust'

print(blackjack(11,11,11))

我预计 print(blackjack(11,11,11)) 的输出是 13,但我没有得到。

【问题讨论】:

  • 根据二十一点的规则,3 A 是 13,而不是 3。
  • 你可以不将 a 或 b 或 c 减少 10,而只需使用一些变量来保存它们的总和,如果一张牌是 Ace,则从该变量中减少 10

标签: python python-2.7


【解决方案1】:
def blackjack(a,b,c):

    Ace_count = [a,b,c].count(11)

    total = sum([a,b,c])

    if total <= 21:
        return ("Not busted", total)

    while total > 21:
        if Ace_count > 0:
            Ace_count -= 1
            total -= 10
        else:
            return "Bust"

    return ("Not busted", total)

print(blackjack(11,11,11))

我认为这应该可行,返回:

('Not busted', 13)

【讨论】:

    【解决方案2】:
    def blackjack(a,b,c):  
        total = a+b+c
        if total <= 21:     
            return total
        elif 11 in [a,b,c] and total > 21:   
            new_total = total-10     
            if new_total > 21:    
                return 'Bust' 
            else:         
                return new_total   
        else: 
            return 'Bust'
    

    【讨论】:

    • 欢迎来到 SO,随着代码的一点点解释提高了答案的质量。
    【解决方案3】:
    def func(x,y,z):
        if sum([x+y+z]) <= 21:
            return sum([x,y,z])
        elif 11 in (x, y, z) and sum((x, y, z)) <= 31:
            i = 0
            while 11 in [x,y,z] and sum([x,y,z]) <= 31:
                i -= 10
            return sum([x,y,z]) - i
        else:
            return 'bust'
    
    print(func(9,9,9)) # bust
    
    print(func(11,11,11)) # 13
    
    print(func(11,10,9,9)) # bust
    

    【讨论】:

      【解决方案4】:
      def blackjack(x,y,z):
          tot=int(x+y+z)
          if tot < 21:
              return tot
          
          elif tot > 21 and x == 11 or y == 11 or z == 11:
              tot2=tot-10
              return tot2
          
          else:
              return "BUST"
              
          
          
      print(blackjack(5, 6, 7))
      print(blackjack(9, 9, 9))
      print(blackjack(9, 9, 11))
      

      【讨论】:

        【解决方案5】:
        def blackjack(numbers):
            if len(numbers) == 3 and max(numbers) <= 11 and min(numbers)>=1:
                sum_n = sum(numbers)
                res =[]
                if (sum_n  <= 21):
                    res = sum_n 
                else:
                    if 11 in numbers:
                        res =sum_n-10
                    else:
                        res = "BUST"
                return res
            else:
                return "numbers length should be 3 and max is 11 and min 1"
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-04-01
          • 2014-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-13
          • 2015-02-12
          相关资源
          最近更新 更多