Answer to Question #306492 in Python for xxx

Question #306492

Question 3

Rules:

1. Play a simple Black Jack game.

2. The player will get two cards first.

3. If the player get black Jack (21 points or two Aces), the player won and does not need to draw the third card.

4. If not, ask if the player wanted to draw the third card.

5. If the player get points less than 22, he won, else he lost.


Points:

1. J, Q and K is 10 points.

2. A is 11 points, but it will become 1 point if the player chooses to draw the third card.

3. Add up the points from the cards and show the final result.


1
Expert's answer
2022-03-06T14:04:58-0500
def print_cards(cards, hidden):

         

    s = ""

    for card in cards:

        s = s + "\t ________________"

    if hidden:

        s += "\t ________________"

    print(s)

 

 

    s = ""

    for card in cards:

        s = s + "\t|                |"

    if hidden:

        s += "\t|                |"    

    print(s)

 

    s = ""

    for card in cards:

        if card.value == '10':

            s = s + "\t|  {}            |".format(card.value)

        else:

            s = s + "\t|  {}             |".format(card.value)  

    if hidden:

        s += "\t|                |"    

    print(s)

 

    s = ""

    for card in cards:

        s = s + "\t|                |"

    if hidden:

        s += "\t|      * *       |"

    print(s)    

 

    s = ""

    for card in cards:

        s = s + "\t|                |"

    if hidden:

        s += "\t|    *     *     |"

    print(s)    

 

    s = ""

    for card in cards:

        s = s + "\t|                |"

    if hidden:

        s += "\t|   *       *    |"

    print(s)    

 

    s = ""

    for card in cards:

        s = s + "\t|                |"

    if hidden:

        s += "\t|   *       *    |"

    print(s)    

 

    s = ""

    for card in cards:

        s = s + "\t|       {}        |".format(card.suit)

    if hidden:

        s += "\t|          *     |"

    print(s)    

 

    s = ""

    for card in cards:

        s = s + "\t|                |"

    if hidden:

        s += "\t|         *      |"

    print(s)    

 

    s = ""

    for card in cards:

        s = s + "\t|                |"

    if hidden:

        s += "\t|        *       |"

    print(s)

 

    s = ""

    for card in cards:

        s = s + "\t|                |"

    if hidden:

        s += "\t|                |"

    print(s)

 

    s = ""

    for card in cards:

        s = s + "\t|                |"

    if hidden:

        s += "\t|                |"

    print(s)    

 

    s = ""

    for card in cards:

        if card.value == '10':

            s = s + "\t|            {}  |".format(card.value)

        else:

            s = s + "\t|            {}   |".format(card.value)

    if hidden:

        s += "\t|        *       |"        

    print(s)    

         

    s = ""

    for card in cards:

        s = s + "\t|________________|"

    if hidden:

        s += "\t|________________|"

    print(s)        


    print()
while len(player_cards) < 2:

 

    # Randomly dealing a card

    player_card = random.choice(deck)

    player_cards.append(player_card)

    deck.remove(player_card)

 

    # Updating the player score

    player_score += player_card.card_value

 

    # In case both the cards are Ace, make the first ace value as 1 

    if len(player_cards) == 2:

        if player_cards[0].card_value == 11 and player_cards[1].card_value == 11:

            player_cards[0].card_value = 1

            player_score -= 10

 

    # Print player cards and score      

    print("PLAYER CARDS: ")

    print_cards(player_cards, False)

    print("PLAYER SCORE = ", player_score)

 

    input()

 

    # Randomly dealing a card

    dealer_card = random.choice(deck)

    dealer_cards.append(dealer_card)

    deck.remove(dealer_card)

 

    # Updating the dealer score

    dealer_score += dealer_card.card_value

 

    # Print dealer cards and score, keeping in mind to hide the second card and score

    print("DEALER CARDS: ")

    if len(dealer_cards) == 1:

        print_cards(dealer_cards, False)

        print("DEALER SCORE = ", dealer_score)

    else:

        print_cards(dealer_cards[:-1], True)    

        print("DEALER SCORE = ", dealer_score - dealer_cards[-1].card_value)

 

 

    # In case both the cards are Ace, make the second ace value as 1 

    if len(dealer_cards) == 2:

        if dealer_cards[0].card_value == 11 and dealer_cards[1].card_value == 11:

            dealer_cards[1].card_value = 1

            dealer_score -= 10

 

    input()

 

# Player gets a blackjack   

if player_score == 21:

    print("PLAYER HAS A BLACKJACK!!!!")

    print("PLAYER WINS!!!!")

    quit()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS