|
| 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 | +import random |
| 32 | + |
| 33 | +face_cards = { |
| 34 | + "K": 10, |
| 35 | + "Q": 10, |
| 36 | + "J": 10, |
| 37 | + "10": 10, |
| 38 | + "9": 9, |
| 39 | + "8": 8, |
| 40 | + "7": 7, |
| 41 | + "6": 6, |
| 42 | + "5": 5, |
| 43 | + "4": 4, |
| 44 | + "3": 3, |
| 45 | + "2": 2, |
| 46 | + "A": 1 |
| 47 | +} |
| 48 | +# #====================== |
| 49 | +# # Input validation fix |
| 50 | +# #====================== |
| 51 | +# while True: |
| 52 | +# try: |
| 53 | +# question_1 = int(input("What's your first card?: ")) |
| 54 | +# question_2 = int(input("What's your second card?: ")) |
| 55 | +# question_3 = int(input("What's your third card?: ")) |
| 56 | +# except ValueError: |
| 57 | +# print("Invalid Value, try agian") |
| 58 | +# continue |
| 59 | +# except TypeError: |
| 60 | +# print("Please enter a number") |
| 61 | +# continue |
| 62 | +# # else: |
| 63 | +# # break |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +card_total = 0 |
| 69 | + |
| 70 | +while True: |
| 71 | + if question_1 in face_cards: |
| 72 | + card_total += int(face_cards[question_1]) |
| 73 | + print(f"question_1: {question_1}") |
| 74 | + |
| 75 | + if question_2 in face_cards: |
| 76 | + card_total += int(face_cards[question_2]) |
| 77 | + print(f"question_2: {question_2}") |
| 78 | + |
| 79 | + if question_3 in face_cards: |
| 80 | + card_total += int(face_cards[question_3]) |
| 81 | + print(f"question_3: {question_3}") |
| 82 | + break |
| 83 | + else: |
| 84 | + print("Not valid") |
| 85 | + break |
| 86 | + else: |
| 87 | + print("Not valid") |
| 88 | + break |
| 89 | + else: |
| 90 | + print("Not valid") |
| 91 | + break |
| 92 | +if card_total < 17: |
| 93 | + print(f"Hit {card_total}") |
| 94 | +elif 17 <= card_total < 21: |
| 95 | + print(f"Stay {card_total}") |
| 96 | +elif card_total == 21: |
| 97 | + print(f"Backjack! {card_total}") |
| 98 | +elif card_total > 21: |
| 99 | + print(f"Already Busted {card_total}") |
| 100 | +else: |
| 101 | + print("Not Valid") |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +# Version 2 (optional) |
| 108 | + |
| 109 | +# Aces can be worth 11 if they won't put the total point value of both cards over 21. Remember that you can have multiple aces in a hand. |
| 110 | +# Try generating a list of all possible hand values by doubling the number of values in the output whenever you encounter an ace. |
| 111 | +# For one half, add 1, for the other, add 11. This ensures if you have multiple aces that you account for the full range of possible values. |
0 commit comments