From 7f439debca1bb76f2ef359190e090b38ada7ad24 Mon Sep 17 00:00:00 2001 From: Derderderr Date: Thu, 12 Feb 2026 00:02:05 +0800 Subject: [PATCH] Refractor add welcome and start_game as seperated function --- blackjack.py | 164 +++++++++++++++++++++++++-------------------------- 1 file changed, 81 insertions(+), 83 deletions(-) diff --git a/blackjack.py b/blackjack.py index b2386ff7828..05f25e1f215 100644 --- a/blackjack.py +++ b/blackjack.py @@ -4,104 +4,102 @@ deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] * 4 -random.shuffle(deck) - -print( - " ********************************************************** " -) -print( - " Welcome to the game Casino - BLACK JACK ! " -) -print( - " ********************************************************** " -) - -d_cards = [] # Initialising dealer's cards -p_cards = [] # Initialising player's cards - -while len(d_cards) != 2: - random.shuffle(deck) - d_cards.append(deck.pop()) - if len(d_cards) == 2: - print("The cards dealer has are X ", d_cards[1]) - -# Displaying the Player's cards -while len(p_cards) != 2: - random.shuffle(deck) - p_cards.append(deck.pop()) - if len(p_cards) == 2: - print("The total of player is ", sum(p_cards)) - print("The cards Player has are ", p_cards) - -if sum(p_cards) > 21: - print("You are BUSTED !\n **************Dealer Wins !!******************\n") - exit() -if sum(d_cards) > 21: +def welcome(): + print( + " ********************************************************** " + ) + print( + " Welcome to the game Casino - BLACK JACK ! " + ) print( - "Dealer is BUSTED !\n ************** You are the Winner !!******************\n" + " ********************************************************** " ) - exit() -if sum(d_cards) == 21: - print("***********************Dealer is the Winner !!******************") - exit() -if sum(d_cards) == 21 and sum(p_cards) == 21: - print("*****************The match is tie !!*************************") - exit() +def start_game(): + random.shuffle(deck) + d_cards = [] + p_cards = [] -def dealer_choice(): - if sum(d_cards) < 17: - while sum(d_cards) < 17: - random.shuffle(deck) - d_cards.append(deck.pop()) + # Dealer initial cards + while len(d_cards) != 2: + random.shuffle(deck) + d_cards.append(deck.pop()) + if len(d_cards) == 2: + print("The cards dealer has are X ", d_cards[1]) + + # Player initial cards + while len(p_cards) != 2: + random.shuffle(deck) + p_cards.append(deck.pop()) + if len(p_cards) == 2: + print("The total of player is ", sum(p_cards)) + print("The cards Player has are ", p_cards) - print("Dealer has total " + str(sum(d_cards)) + "with the cards ", d_cards) + if sum(p_cards) > 21: + print("You are BUSTED !\n **************Dealer Wins !!******************\n") + return - if sum(p_cards) == sum(d_cards): - print("***************The match is tie !!****************") - exit() + if sum(d_cards) > 21: + print( + "Dealer is BUSTED !\n ************** You are the Winner !!******************\n" + ) + return + + if sum(d_cards) == 21 and sum(p_cards) == 21: + print("*****************The match is tie !!*************************") + return if sum(d_cards) == 21: - if sum(p_cards) < 21: - print("***********************Dealer is the Winner !!******************") - elif sum(p_cards) == 21: - print("********************There is tie !!**************************") - else: - print("***********************Dealer is the Winner !!******************") + print("***********************Dealer is the Winner !!******************") + return - elif sum(d_cards) < 21: - if sum(p_cards) < 21 and sum(p_cards) < sum(d_cards): - print("***********************Dealer is the Winner !!******************") - if sum(p_cards) == 21: - print("**********************Player is winner !!**********************") - if sum(p_cards) < 21 and sum(p_cards) > sum(d_cards): - print("**********************Player is winner !!**********************") + def dealer_choice(): + if sum(d_cards) < 17: + while sum(d_cards) < 17: + random.shuffle(deck) + d_cards.append(deck.pop()) + + print("Dealer has total " + str(sum(d_cards)) + " with the cards ", d_cards) - else: - if sum(p_cards) < 21: + if sum(p_cards) == sum(d_cards): + print("***************The match is tie !!****************") + return + + if sum(d_cards) > 21: print("**********************Player is winner !!**********************") - elif sum(p_cards) == 21: + return + + if sum(d_cards) > sum(p_cards): + print("***********************Dealer is the Winner !!******************") + else: print("**********************Player is winner !!**********************") + + # Player turn + while sum(p_cards) < 21: + k = input("Want to hit or stay?\n Press 1 for hit and 0 for stay ") + + if k == "1": + random.shuffle(deck) + p_cards.append(deck.pop()) + print("You have a total of " + str(sum(p_cards)) + " with the cards ", p_cards) + + if sum(p_cards) > 21: + print("*************You are BUSTED !*************\n Dealer Wins !!") + return + + if sum(p_cards) == 21: + print( + "*******************You are the Winner !!*****************************" + ) + return else: - print("***********************Dealer is the Winner !!******************") + dealer_choice() + break -while sum(p_cards) < 21: - k = input("Want to hit or stay?\n Press 1 for hit and 0 for stay ") - if k == 1: - random.shuffle(deck) - p_cards.append(deck.pop()) - print("You have a total of " + str(sum(p_cards)) + " with the cards ", p_cards) - if sum(p_cards) > 21: - print("*************You are BUSTED !*************\n Dealer Wins !!") - if sum(p_cards) == 21: - print( - "*******************You are the Winner !!*****************************" - ) - - else: - dealer_choice() - break +# Run Game +welcome() +start_game()