Py学习  »  Python

python 21点:如何构造if语句以确定是否应将ace转换为1?

Troy • 6 年前 • 1774 次点击  

我试图解决一个简单的编码练习:给定三个介于1和11之间的整数,如果它们的和小于或等于21,则返回它们的和。如果它们的总和超过21,并且有元素,则减少11。最后,如果总和(即使在调整后)超过21,则返回“半身像”

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))

我原以为打印输出(21点(11,11,11))是13,但我一个也没有得到。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46269
文章 [ 1 ]  |  最新文章 6 年前