理解 ‘and’ 和 ‘or’ 的运作方式。

huangapple go评论64阅读模式
英文:

Understanding the functioning of 'and' and 'or'

问题

这是一个我正在面临的疑问。

这个代码以文档字符串中的目的如下:

这是正确的代码,但我对blackjack_hand_greater_than(a, b)函数中最后的'if语句'感到困惑。

在这里,'if语句'对于total_1 > total_2 即使为真(通过打印语句进行了检查),也是不为真。我不明白为什么要添加'或 total_2 > 21'。

def blackjack_hand_greater_than(hand_1, hand_2):
    """
    Return True if hand_1 beats hand_2, and False otherwise.
    
    In order for hand_1 to beat hand_2 the following must be true:
    - The total of hand_1 must not exceed 21
    - The total of hand_1 must exceed the total of hand_2 OR hand_2's total must exceed 21
    
    Hands are represented as a list of cards. Each card is represented by a string.
    
    When adding up a hand's total, cards with numbers count for that many points. Face
    cards ('J', 'Q', and 'K') are worth 10 points. 'A' can count for 1 or 11.
    
    When determining a hand's total, you should try to count aces in the way that 
    maximizes the hand's total without going over 21. e.g. the total of ['A', 'A', '9'] is 21,
    the total of ['A', 'A', '9', '3'] is 14.
    
    Examples:
    >>> blackjack_hand_greater_than(['K'], ['3', '4'])
    True
    >>> blackjack_hand_greater_than(['K'], ['10'])
    False
    >>> blackjack_hand_greater_than(['K', 'K', '2'], ['3'])
    False
    """
    print("hand1 = ", hand_1)
    print("hand2 = ", hand_2)
    total_1 = get_total(hand_1)
    total_2 = get_total(hand_2)
    print(total_1 <= 21)
    print(total_1 > total_2)
    if (total_1 <= 21) and (total_1 > total_2 or total_2 > 21):
        return True
    else:
        return False

def get_total(hands) :
    values = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 10, 'Q': 10, 'K': 10}
    total = 0
    aces = 0
    
    for x in hands:
        if x == 'A' :
            aces += 1
        total += values[x]
        
    while aces > 0 and total + 10 <= 21:
        total += 10
        aces -= 1
    
    print(total)
    return total
# Check your answer
q3.check()

在移除**'or'**的第二个操作数后,出现错误如下:

hand1 = ['J', 'A']
hand2 = ['6']
21
6
True
True

hand1 = ['9']
hand2 = ['9', 'Q', '8', 'A']
9
28
True
False

不正确:给定hand_1=['9'],hand_2=['9', 'Q', '8', 'A'],预期返回True,但实际返回False。

英文:

Here's a doubt I am facing here.

The code with its purpose in docstring is below :

This is the correct code but I am confused about the final 'if statement' in the blackjack_hand_greater_than(a,b) function.

Here, the 'if statement' is not True for total_1 &gt; total_2 even if it is(* checked through print statements). I am not getting what is the need of adding 'or total_2 &gt; 21' .

def blackjack_hand_greater_than(hand_1, hand_2):
&quot;&quot;&quot;
Return True if hand_1 beats hand_2, and False otherwise.
In order for hand_1 to beat hand_2 the following must be true:
- The total of hand_1 must not exceed 21
- The total of hand_1 must exceed the total of hand_2 OR hand_2&#39;s total must exceed 21
Hands are represented as a list of cards. Each card is represented by a string.
When adding up a hand&#39;s total, cards with numbers count for that many points. Face
cards (&#39;J&#39;, &#39;Q&#39;, and &#39;K&#39;) are worth 10 points. &#39;A&#39; can count for 1 or 11.
When determining a hand&#39;s total, you should try to count aces in the way that 
maximizes the hand&#39;s total without going over 21. e.g. the total of [&#39;A&#39;, &#39;A&#39;, &#39;9&#39;] is 21,
the total of [&#39;A&#39;, &#39;A&#39;, &#39;9&#39;, &#39;3&#39;] is 14.
Examples:
&gt;&gt;&gt; blackjack_hand_greater_than([&#39;K&#39;], [&#39;3&#39;, &#39;4&#39;])
True
&gt;&gt;&gt; blackjack_hand_greater_than([&#39;K&#39;], [&#39;10&#39;])
False
&gt;&gt;&gt; blackjack_hand_greater_than([&#39;K&#39;, &#39;K&#39;, &#39;2&#39;], [&#39;3&#39;])
False
&quot;&quot;&quot;
print(&quot;hand1 = &quot;,hand_1)
print(&quot;hand2 = &quot;,hand_2)
total_1 = get_total(hand_1)
total_2 = get_total(hand_2)
print(total_1 &lt;= 21)
print(total_1 &gt; total_2)
if (total_1 &lt;= 21) and (total_1&gt;total_2 or total_2 &gt; 21):
return True
else :
return False
def get_total(hands) :
values = {&#39;A&#39;: 1 ,&#39;2&#39;: 2, &#39;3&#39; : 3, &#39;4&#39; : 4 ,&#39;5&#39; : 5, &#39;6&#39; : 6,&#39;7&#39;: 7, &#39;8&#39; : 8, &#39;9&#39; : 9, &#39;10&#39; : 10 , &#39;J&#39; :10 , &#39;Q&#39;:10, &#39;K&#39;:10}
total = 0
aces = 0
for x in hands:
if x == &#39;A&#39; :
aces += 1
total += values[x]
#         print(total)
while aces&gt;0 and total + 10 &lt;= 21 :
total += 10
aces -=1
print(total)
return total
# Check your answer
q3.check()

The error after removing the 2nd operand of 'or', the error received is as follows :

hand1 = ['J', 'A']
hand2 = ['6']
21
6
True
True

hand1 = ['9']
hand2 = ['9', 'Q', '8', 'A']
9
28
True
False

Incorrect: Expected return value of True given hand_1=['9'], hand_2=['9', 'Q', '8', 'A'], but got False instead.

答案1

得分: 0

要赢得二十一点游戏(blackjack),你需要比庄家的点数接近二十一点,但不能超过二十一点。如果你的点数超过二十一点(称为“爆牌”),你就输了,无论庄家手上的牌有多少点。如果你的点数是二十一点或以下,你会赢得游戏,如果庄家爆牌,或者庄家没有爆牌但点数比你少。如果我们用你的手牌分数表示为 total_1,庄家手牌的总点数表示为 total_2,那么这在逻辑上等同于 if (total_1 <= 21) and (total_1 > total_2 or total_2 > 21):如果你爆牌,第一个条件将为 False,由于我们处于一个合取运算中,整个表达式将评估为 False。如果你的点数是二十一点或以下,你仍然没有确切地赢得游戏 - 你需要要么比庄家多,要么庄家爆牌。这构成了合取运算中的第二个条件:total_1 > total_2 or total_2 > 21

换句话说,问题不在于编码或逻辑,而在于二十一点游戏的规则。

英文:

To win a blackjack hand, you need to be closer to 21 than the dealer but not above 21. If you're above 21 ("bust"), you lose, no matter which hand the dealer has. If you're at 21 or below, you win if the dealer busts, or if the dealer does not bust but has fewer points than you do. If we denote the score of your hand as total_1 and the total of the dealer's hand as total_2, then this is logically equivalent to if (total_1 &lt;= 21) and (total_1&gt;total_2 or total_2 &gt; 21): If you bust, the first condition will be False, and since we're in a conjunction, the overall expression will evaluate to False. If you're at 21 or below, you still haven't won for sure - you need to either have more than the dealer, or the delaer must bust. That's the disjunction that forms the second term in the conjunction: total_1&gt;total_2 or total_2 &gt; 21.

In other words, the issue is not one of coding or of logic, but of blackjack rules.

答案2

得分: 0

以下是翻译好的部分:

The 'if statement':

if (total_1 <= 21) and (total_1>total_2 or total_2 > 21):
    return True
else:
    return False

它表示如果(total_1小于或等于) 并且 (total_1大于total_2) (total_2大于21)

例如,在组合中,它会看起来像这样:

if (True) and (True or False):# 这将返回True,因为`True and (True or False)为True`
    return True
else:
    return False
英文:

The 'if statement':

if (total_1 &lt;= 21) and (total_1&gt;total_2 or total_2 &gt; 21):
    return True
else:
    return False

It is saying that if (total_1 is less than or equal to) and (total_1 is greater than total_2) or (total_2 greater than 21)

for example in the the combinations of it would look like this:

if (True) and (True or False):# this would return True because `True and (True or False) make True` 
return True
else:
return False

答案3

得分: 0

如果你让 ( ) 保持原样 if (total_1 &lt;= 21) and (total_1&gt;total_2 or total_2 &gt; 21):,那么 or 条件将只在其中一个操作数为 True 时返回 True(total_2 > 21),因为 total_1 <= 21 也为 True,整个表达式将被评估为 True。毫无疑问,这是正确的。

但是... 让我们去掉条件中的括号 ( ),看看会发生什么。从逻辑上看,and 的第一个操作数为 True,但第二个操作数为 False,所以... 条件应该评估为 False,对吗?但是运行以下代码:

total_1 =  9; total_2 = 28
if total_1 &lt;= 21 and total_1&gt;total_2 or total_2 &gt; 21:
    print(True)
else :
    print(False)

会打印 True ...

这是怎么回事?

Python 从左到右评估表达式。首先,and 条件将被评估为 False,但... 表达式中还有一个 or,因此来自 and 条件的 False 现在是 or 条件的操作数,而第二个条件为 True,所以整个表达式最终评估为 True。

这是你关于链式使用 andor 的疑惑吗?

英文:

I think I may have got what you are actually wondering about:

If you let the ( ) stay as they are if (total_1 &lt;= 21) and (total_1&gt;total_2 or total_2 &gt; 21):, it is strictly logical that the or condition will return True if only ONE of the operands is True ( total_2 > 21 ) and because total_1 <= 21 is also True the entire expression evaluates to True. No doubt that this is correct.

BUT ... let's remove the parentheses ( ) in the condition and see what happens. Logically the first operand for and is True, but the second is False, so ... the condition should evaluate to False, right? But running the code:

total_1 =  9; total_2 = 28
if total_1 &lt;= 21 and total_1&gt;total_2 or total_2 &gt; 21:
print(True)
else :
print(False)

prints True ...

How does it come?

Python evaluates the expressions from left to the right. So first the and condition will be evaluated and gives False, BUT ... the expression has also an or, so the False from the and condition is now the operand of the or condition which second term is True, so, that the entire expression evaluates then to True.

Is THIS what you are wondering about considering chained and and or?

Check again from scratch: Expected return value of True given hand_1=['9'], hand_2=['9', 'Q', '8', 'A'], but got False instead. Then you will see that it actually can't be as you have it in mind.

Another problem with understanding can be wrong assumptions about the rules of the game as pointed out by Schnitte:

> the issue is not one of coding or of logic, but of blackjack rules.

答案4

得分: 0

我解决了它的方法:

1-我将数字和字母分开
2-按字母顺序排序字母,A是最后一个

def sum_hand(hand):
    #hand.sort(reverse=True)

    letter = []
    number = []
    for n in hand:
        if n.isnumeric():
            number.append(n)
        else:
            letter.append(n)

    letter.sort(reverse=True)
    number.sort(reverse=True)
    number.extend(letter)

    hand = number
    #hand = sorted(number) + sorted(letter,reverse:True)
    print(hand)

    result_hand = 0

    for card in hand:
        if (card == 'J' or card == 'Q' or card == 'K'):
            result_hand += 10
        elif (card.isdigit()):
            result_hand += int(card)
        else:
            if (result_hand <= 10):
                result_hand += 10
            else:
                result_hand += 1

    return result_hand

def blackjack_hand_greater_than(hand_1, hand_2):
    result_hand1 = sum_hand(hand_1)
    result_hand2 = sum_hand(hand_2)

    print('hand1-', result_hand1, 'hand2', result_hand2)

    if result_hand1 > 21:
        return False

    if result_hand2 > 21:
        return True

    if (result_hand1 > result_hand2):
        return True
    else:
        return False
英文:

Im shared how can I solved it

1-I split the numbers and letters
2-I sorted the letter in order letter A is the last one

def sum_hand(hand):
#hand.sort(reverse=True)
letter=[]
number=[]
for n in hand:
if n.isnumeric():
number.append(n)
else:
letter.append(n)
#print(&#39;letter--&#39;,letter)
#print(&#39;number--&#39;,number)
letter.sort(reverse=True)
number.sort(reverse=True)
number.extend(letter)
hand = number
#hand = sorted(number) + sorted(letter,reverse:True)
print(hand)
#print(hand)
result_hand=0
for card in hand:
if (card == &#39;J&#39; or card == &#39;Q&#39; or card == &#39;K&#39;):
result_hand += 10
elif (card.isdigit()):
result_hand += int(card)
else:
if(result_hand &lt;= 10):
result_hand += 10
else:
result_hand += 1
return result_hand
def blackjack_hand_greater_than(hand_1, hand_2):
result_hand1 = sum_hand(hand_1)
result_hand2 = sum_hand(hand_2)
print(&#39;hand1-&#39;, result_hand1 , &#39; hand2&#39;, result_hand2)
if result_hand1 &gt; 21:
return False
if result_hand2 &gt; 21:
return True
if (result_hand1 &gt; result_hand2 ):
return True
else:
return False

huangapple
  • 本文由 发表于 2023年2月6日 04:36:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定