Skip to content

Commit caf3131

Browse files
authored
Merge pull request #68 from PdxCodeGuild/daniel-lab04-blackjackAdvice
Daniel lab04 blackjack advice
2 parents 74ce5d5 + 2667014 commit caf3131

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.

code/daniel/lab07-ccv/lab07-ccv.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Lab 7: Credit Card Validation
2+
3+
# Let's write a function credit_card_validator which returns whether a string containing a credit card number is valid as a boolean. The steps are as follows:
4+
5+
# Convert the input string into a list of ints
6+
# Slice off the last digit. That is the check digit.
7+
# Reverse the digits.
8+
# Double every other element in the reversed list (starting with the first number in the list).
9+
# Subtract nine from numbers over nine.
10+
# Sum all values.
11+
# Take the second digit of that sum.
12+
# If that matches the check digit, the whole card number is valid.
13+
14+
# Here is a valid credit card number to test with: 4556737586899855
15+
16+
# For example, the worked out steps would be:
17+
18+
# 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5 5
19+
# 4 5 5 6 7 3 7 5 8 6 8 9 9 8 5
20+
# 5 8 9 9 8 6 8 5 7 3 7 6 5 5 4
21+
# 10 8 18 9 16 6 16 5 14 3 14 6 10 5 8
22+
# 1 8 9 9 7 6 7 5 5 3 5 6 1 5 8
23+
# 85
24+
# 5
25+
# True Valid!
26+
# ========================================================================================
27+
28+
29+
enter_ccn = (input("Enter your 16-digit credit card number: "))
30+
# print(len(enter_ccn))
31+
32+
33+
try:
34+
len(enter_ccn) == 16
35+
enter_ccn = list(enter_ccn)
36+
print(enter_ccn)
37+
# print("check")
38+
except (ValueError):
39+
print("Invalid number length, try again")
40+
print(enter_ccn)
41+
except (ValueError):
42+
print('Please enter a number')
43+
# ====================================================
44+
# Convert the input string into a list of ints
45+
# ====================================================
46+
# print(enter_ccn)
47+
# print(type(enter_ccn))
48+
49+
# ====================================================
50+
# Slice off the last digit. That is the check digit.
51+
# ====================================================
52+
check_digit = enter_ccn.pop()
53+
print(check_digit)
54+
55+
# ====================================================
56+
# Reverse the digits.
57+
# ====================================================
58+
enter_ccn.reverse()
59+
print(enter_ccn)
60+
61+
# =============================================================================================
62+
# Double every other element in the reversed list (starting with the first number in the list).
63+
# =============================================================================================
64+
65+
index = 0
66+
ccn_odd_doubled = []
67+
for odd in enter_ccn:
68+
if int(enter_ccn[index]) % 2 != 0:
69+
ccn_odd_doubled.append(int(enter_ccn[index]) * 2)
70+
index += 1
71+
else:
72+
ccn_odd_doubled.append(int(enter_ccn[index]))
73+
index += 1
74+
print(ccn_odd_doubled)
75+
76+
77+
# ====================================================
78+
# Subtract nine from numbers over nine.
79+
# ====================================================
80+
index = 0
81+
for num in ccn_odd_doubled:
82+
if ccn_odd_doubled[index] >= 9:
83+
ccn_odd_doubled.pop(index)
84+
index += 1
85+
print(ccn_odd_doubled)
86+
# ====================================================
87+
# Sum all values.
88+
# ====================================================
89+
index = 0
90+
sum_ccn = 0
91+
for num in ccn_odd_doubled:
92+
sum_ccn += ccn_odd_doubled[index]
93+
index += 1
94+
print(sum_ccn)
95+
96+
# ====================================================
97+
# Take the second digit of that sum.
98+
# ====================================================
99+
index = 1
100+
second_digit = list(map(int, str(sum_ccn)))
101+
# print(str(second_digit))
102+
print(second_digit[index])
103+
104+
105+
# ====================================================
106+
# If that matches the check digit, the whole card number is valid.
107+
# ====================================================
108+
109+
if check_digit == second_digit[index]:
110+
print("True Valid!")
111+
else:
112+
print("False, check digit not valid")

0 commit comments

Comments
 (0)