|
| 1 | +# Lab 4: Blackjack Advice |
| 2 | + |
| 3 | +# Let's write a python program to give basic blackjack playing advice during a game by asking the player for cards. |
| 4 | +# First, ask the user for three playing cards (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, or K). |
| 5 | +# Then, figure out the point value of each card individually. Number cards are worth their number, all face cards are worth 10. |
| 6 | +# At this point, assume aces are worth 1. Use the following rules to determine the advice: |
| 7 | + |
| 8 | +# Less than 17, advise to "Hit" |
| 9 | +# Greater than or equal to 17, but less than 21, advise to "Stay" |
| 10 | +# Exactly 21, advise "Blackjack!" |
| 11 | +# Over 21, advise "Already Busted" |
| 12 | + |
| 13 | +# Print out the current total point value and the advice. |
| 14 | + |
| 15 | +# What's your first card? Q |
| 16 | +# What's your second card? 2 |
| 17 | +# What's your third card? 3 |
| 18 | +# 15 Hit |
| 19 | + |
| 20 | +# What's your first card? K |
| 21 | +# What's your second card? 5 |
| 22 | +# What's your third card? 5 |
| 23 | +# 20 Stay |
| 24 | + |
| 25 | +# What's your first card? Q |
| 26 | +# What's your second card? J |
| 27 | +# What's your third card? A |
| 28 | +# 21 Blackjack! |
| 29 | + |
| 30 | + |
| 31 | +# Then, figure out the point value of each card individually. Number cards are worth their number, all face cards are worth 10. |
| 32 | +player_cards = [] |
| 33 | + |
| 34 | +# First, ask the user for three playing cards (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, or K). |
| 35 | + |
| 36 | +for player_card in range(1, 4): |
| 37 | + player_cards.append(input(f"What's your card #{player_card}: ").upper()) |
| 38 | + |
| 39 | +# At this point, assume aces are worth 1. Use the following rules to determine the advice: |
| 40 | + |
| 41 | +# Less than 17, advise to "Hit" |
| 42 | +# Greater than or equal to 17, but less than 21, advise to "Stay" |
| 43 | +# Exactly 21, advise "Blackjack!" |
| 44 | +# Over 21, advise "Already Busted" |
| 45 | + |
| 46 | + |
| 47 | +def summ(nums): |
| 48 | + total = 0 |
| 49 | + cards = { |
| 50 | + "A": 1, |
| 51 | + "2": 2, |
| 52 | + "3": 3, |
| 53 | + "4": 4, |
| 54 | + "5": 5, |
| 55 | + "6": 6, |
| 56 | + "7": 7, |
| 57 | + "8": 8, |
| 58 | + "9": 9, |
| 59 | + "10": 10, |
| 60 | + "J": 10, |
| 61 | + "Q": 10, |
| 62 | + "K": 10, |
| 63 | + } |
| 64 | + |
| 65 | + for num in nums: |
| 66 | + total = total + cards[num] |
| 67 | + return total |
| 68 | + |
| 69 | + |
| 70 | +cards_total = summ(player_cards) |
| 71 | + |
| 72 | +if cards_total < 17: |
| 73 | + result = "Hit" |
| 74 | +elif cards_total >= 17 and cards_total < 21: |
| 75 | + result = "Stay" |
| 76 | +elif cards_total == 21: |
| 77 | + result = "Blackjack!" |
| 78 | +else: |
| 79 | + result = "Already Busted" |
| 80 | + |
| 81 | +# Print out the current total point value and the advice. |
| 82 | + |
| 83 | +print(f"{cards_total} is a {result}") |
0 commit comments