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.