Answer to Question #307177 in Python for xxx

Question #307177

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.


Code:

import random

cards = "A234567890JQK"

print("Your cards are: - -")

first_card = random.randint(1, 13)



1
Expert's answer
2022-03-07T16:45:23-0500
import random

cards = "A234567890JQK"

def check_points(card, third):
    if card == 'A':
        if third:
            return 1
        else:
            return 11
    elif card == '0' or card == 'J' or card == 'Q' or card == 'K':
        return 10
    else:
        return int(card)

first_card = random.randint(0, 12)
second_card = random.randint(0, 12)
result = check_points(cards[first_card], False) + check_points(cards[second_card], False)
print("Your cards are: ", cards[first_card], ' ' , cards[second_card], ' Your result: ', result)

if cards[first_card]+cards[second_card] == 'AA' or result == 21:
    print('You won!')
else:
    if input('Do you want to take the third card?(Y/N)').lower() == 'y':
        third_card = random.randint(0, 12)
        result += check_points(cards[third_card], True)
        print("Your card is: ", cards[third_card], ' Your result: ', result)
        if result < 22:
            print('You won!')
        else:
            print('You lose!')
    else:
        print('Your result: ', result)

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