`Total1` 变量与 `DealerTotal` 变量具有相同的输出,但它们应该不同

`Total1` variable has the same output as `DealerTotal` variable, when they should be different

这是我的 Blackjack 修改版。

我对Python还是个新手,所以这个程序缺少格式化和严重的规则修改,但我不明白的是如果你要运行这个程序为什么 Total1DealerTotal.

的输出相同

在第一阶段,如果你只是通过并确认你通过了,那么一切都会很顺利。您有自己的总数,即 Card1Card2 的总和,庄家有他自己的总数(我作弊并让他中了 3 次):Card1Card2Card3。所以在 PHASE 1.

中一切正常

但是当我输入 PHASE 2 时,执行相同的过程(传递和确认)会使 Total1DealerTotal 输出相同。

为什么 PHASE 1 中的 DealerTotal 有效,而 PHASE 2 中的 DealerTotal 即使代码相同也无效?

# Imports
import random
import sys

# Dictionary
Cards = {"1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9,
         "10": 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 10}

# Welcome Print
print('Welcome to Blackjack!\n\nHere are your cards: \n ')
# Cards
keys = list(Cards)
# Beginning Cards
Card1 = random.choice(keys)
Card2 = random.choice(keys)
# Hit 1 Card
Card3 = random.choice(keys)
# Hit 2 Card
Card4 = random.choice(keys)
# Hit 3 Card
Card5 = random.choice(keys)
# Hit 4 Card
Card6 = random.choice(keys)
# Hit 5 Card
Card7 = random.choice(keys)
# Hit 6 Card
Card8 = random.choice(keys)

# Sum of Cards
# Beginning Total
Total = Cards[Card1] + Cards[Card2]
# Hit 1 Total
Total1 = Total + Cards[Card3]
# Hit 2 Total
Total2 = Total1 + Cards[Card4]
# Hit 3 Total
Total3 = Total2 + Cards[Card5]
# Hit 4 Total
Total4 = Total3 + Cards[Card6]
# Dealer's Total
DealerTotal = Cards[Card1] + Cards[Card2] + Cards[Card3]



### BEGINNING ###
print(Card1, 'and a', Card2, '. Your total is', Total)


### PHASE 1 ###
Choice = input('Would you like to hit or pass? (h = hit, p = pass): ')
# Hitting PHASE 1
if (Choice == 'h'):
    print('The dealer gives you a', Card3, '. Your new total is: '  , Total1)
    # Hitting & Losing HIT 1
    if (Total1 > 21):
        print('You lose! Try again later.')
        sys.exit()
# Passing PHASE 1
if (Choice == 'p'):
    # Confirmation whether you want to pass/hit
    Response1 = input('Are you sure? The dealer may have a better hand. (y = yes, n = no): ')
    # Reponse to hitting 'y'
    if (Response1 == 'y'):
        print('Your total is', Total, "The dealer's total is", DealerTotal)
        if (DealerTotal > 21 or 21 >= Total > DealerTotal):
            print("Congratulations, you've won!")
            sys.exit()
        elif (DealerTotal == Total):
            print('You have tied!')
        else:
            print('You lose! Try again later.')
            sys.exit()


### PHASE 2 ###
Choice1 = input('Would you like to hit or pass? (h = hit, p = pass): ')
# Hitting PHASE 2
if (Choice1 == 'h'):
    print('The dealer gives you a', Card4, '. Your new total is: ' , Total2)
    # Hittnig & Losing HIT 2
    if (Total2 > 21):
        print('You lose! Try again later.')
        sys.exit()
# Passing PHASE 2
if (Choice1 == 'p'):
    # Confirmation whether you want to pass/hit
    Response2 = input('Are you sure? The dealer may have a better hand.(y = yes, n = no): ')
    # Reponse to hitting 'y'
    if (Response2 == 'y'):
        print('Your total is', Total1, "The dealer's total is", DealerTotal)
        if (DealerTotal > 21 or 21 >= Total1 > DealerTotal):
            print("Congratulations, you've won!")
            sys.exit()
        elif (DealerTotal == Total1):
            print('You have tied!')
        else:
            print('You lose! Try again later.')
            sys.exit()

这是因为你写的逻辑。

Total = Cards[Card1] + Cards[Card2]
Total1 = Total + Cards[Card3]

替换总计的值

Total1 = Cards[Card1] + Cards[Card2] + Cards[Card3]

DealerTotal = Cards[Card1] + Cards[Card2] + Cards[Card3]

正如您可能看到的,DealerTotal = Total1,这就是为什么它们是相同的。

这听起来可能很愚蠢,但 Total1 与 DealerTotal 具有相同值的原因是因为 Total1 与 DealerTotal 具有相同的值。

您在程序开始时分配了变量,然后再也没有碰过它们。总计 1 是 Total1 = Total + Cards[Card3]。总计 Total = Cards[Card1] + Cards[Card2]。所以,Total1 等同于:Cards[Card1] + Cards[Card2] + Cards[Card3].

现在让我们看看 DealerTotal:DealerTotal = Cards[Card1] + Cards[Card2] + Cards[Card3]。似曾相识?

第 1 阶段不会发生这种情况,因为您使用 Total 进行比较,而不是 Total1