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

45 views Asked by At

This is my modified version of Blackjack.

I'm still new to Python, so there's a lack of formatting in this program and serious rule modification, but what I don't understand is if you were to run this program why Total1 has the same output as DealerTotal.

In Phase 1, if you just pass and confirm that you're passing, everything works out smoothly. You have your own total, which is the sum of Card1 and Card2 and the dealer has his own total (which I'm cheating and having him hit 3 times): Card1, Card2, Card3. So everything works fine in PHASE 1.

But when I enter PHASE 2, doing the same process (passing & confirming) makes the Total1 and DealerTotal output identical.

Why does the DealerTotal in PHASE 1 work, but the DealerTotal in PHASE 2 not even when the code is identical?

# 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()
2

There are 2 answers

0
Namit Singal On BEST ANSWER

This is because of the logic that you have written.

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

Replacing the value of Total

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

and

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

as you can probably see, DealerTotal = Total1, that is why they are coming as identical.

1
pcl On

This might sound dumb, but the reason Total1 has the same value as DealerTotal is because Total1 has the same value as DealerTotal.

You assign the variables at the start of the program and then never touch them again. Total1 is Total1 = Total + Cards[Card3]. Total is Total = Cards[Card1] + Cards[Card2]. So, Total1 is equivalent to: Cards[Card1] + Cards[Card2] + Cards[Card3].

Now let's look at DealerTotal: DealerTotal = Cards[Card1] + Cards[Card2] + Cards[Card3]. Seem familiar?

This isn't happening in Phase 1 because you're using Total for the comparison, rather than Total1