Conditional logic in python

import random

class BlackJack:
    def __init__(self,
                 u_hand=list(random.choices(range(1, 11), k=2)),
                 u_dealer=list(random.choices(range(1, 11), k=2))):
        self.u_hand = u_hand
        self.u_dealer = u_dealer

    def print_cards(self, cards):
        return ["\t".join(map(str, cards[i:i+2])) for i in range(0, len(cards), 2)]

    def game_starts(self):
        print(f"Your hand: {self.u_hand}\n"
              f"Dealer's hand: {self.u_dealer[0]}")

        while sum(self.u_hand) < 22:
            if input("hit or stand\n") != "stand":
                self.u_hand.append(random.randint(1, 10))
                print("Your cards are:")
                for zo in self.print_cards(self.u_hand):
                    print(zo)
            else:
                break

        if sum(self.u_hand) > 21:
            return "You Bust"

        while sum(self.u_dealer) < 18:
            print("Dealer's cards are:")
            self.u_dealer.append(random.randint(1, 10))
            for oz in self.print_cards(self.u_dealer):
                print(oz)
        if sum(self.u_dealer) > 21:
            return "Bust"
        return (f"Your card's total:\n{sum(self.u_hand)}\n"
                f"Dealer's card's total:\n{sum(self.u_dealer)}")

    def game_ends(self):
        if sum(self.u_hand) < sum(self.u_dealer) < 22:
            return "Dealer wins!"
        elif sum(self.u_dealer) < sum(self.u_hand) < 22:
            return "You win!"
        elif sum(self.u_dealer) == sum(self.u_hand):
            return "Draw!"

Game_result = BlackJack().game_starts()
# if Game_result != "You Bust":
#     print(Game_result,"\n",BlackJack().game_ends())
# else:
#     print(Game_result)
print(Game_result,"\n",BlackJack().game_ends() if Game_result != "You Bust" else Game_result)

why there is a difference in the outputs of both the methods of conditional logic?
The second one gives the “You Bust” twice whereas the first one does it job pretty well.

whenever you do BlackJack() you are creating a new instance, maybe you want to keep playing with only one instance? a new instance means that self.u_hand and self.u_dealer are calculated again and they are different from before.

I suggest you do

game = BlackJack()

and then call the methods on game so you don’t have many different hands of cards to deal with

I didn’t understand, please make it simpler.

BlackJack is a class, everytime you call BlackJack() you create a new instance of the class with different values of u_hand and u_dealer

Try

game1 = BlackJack()
print(game1.u_hand)
print(game1.u_dealer)

game2 = BlackJack()
print(game2.u_hand)
print(game2.u_dealer)

game2 = BlackJack()
print(game2.u_hand)
print(game2.u_dealer)

so if you want to have always the same game you should use the call to the class BlackJack() only once

if I call the class whether it is instantiated or not it will act in a similar manner.

calling BlackJack().u_hand twice and
calling game2().u_hand twice

makes no difference, right?

Which two methods? The commented out if/else and the print in last line? Why do you expect last print to print just single line?

Because the structure of both the code is same.

print(
    Game_result,
    "\n",
    BlackJack().game_ends() if Game_result != "You Bust" else Game_result
)

That’s three things that will be printed every time.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.