from random import choice as rc
def total(hand):
aces = hand.count(11)
t = sum(hand)
if t > 21 and aces > 0:
while aces > 0 and t > 21:
t -= 10
aces -= 1
return t
cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
cwin = 0
pwin = 0
while True:
player = []
player.append(rc(cards))
player.append(rc(cards))
pbust = False
cbust = False
while True:
tp = total(player)
print ("The player has these cards %s with a total value of %d" % (player, tp))
if tp > 21:
print ("--> The player is busted!")
pbust = True
break
elif tp == 21:
print ("\a BLACKJACK!!!")
break
else:
hs = input("Hit or Stand/Done (h or s): ").lower()
if 'h' in hs:
player.append(rc(cards))
else:
break
while True:
comp = []
comp.append(rc(cards))
comp.append(rc(cards))
while True:
tc = total(comp)
if tc < 18:
comp.append(rc(cards))
else:
break
print ("the computer has %s for a total of %d" % (comp, tc))
if tc > 21:
print ("--> The computer is busted!")
cbust = True
if pbust == False:
print ("The player wins!")
pwin += 1
elif tc > tp:
print ("The computer wins!")
cwin += 1
elif tc == tp:
print ("It's a draw!")
elif tp > tc:
if pbust == False:
print ("The player wins!")
pwin += 1
elif cbust == False:
print ("The computer wins!")
cwin += 1
break
print ("Wins, player = %d computer = %d" % (pwin, cwin))
exit = input("Press Enter (q to quit): ").lower()
if 'q' in exit:
break
print ("Thanks for playing blackjack with the computer!")
I'm new to python. Just a beginner. This is not my actual code. I did some tweak into it. But mostly the overall function and the way it executes is almost similar. Instead of showing just numbers, is there any way to change to something like this: "Spades", "Clubs", "Hearts", "Diamonds", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" but still able to transfer the value represented to be integrated into part of the code. I hope can improve my codes as time passes. I'm not really familiar with classes yet, although it will make the code looks clean. Hope anyone can point out mistakes or making some improvement.
I think you should start with making a deck instead of just numbers:
import itertools
suits = ["hearts", "spades", "diamonds", "clubs"]
cards = [("two", 2),
("three", 3),
("four", 4),
("five", 5),
("six", 6),
("seven", 7),
("eight", 8),
("nine", 9),
("ten", 10),
("Jack", 10),
("Queen", 10),
("King", 10),
("Ace", 11)]
deck = list(itertools.product(suits, cards))
Then you can index in depending on what you want. When counting you can select the value, when printing you can look at the name and suit.
Some general programming tips:
while True:
loops in this program is scary. Only use those when you absolutely have to because they are very bug-prone.player_total
instead of tp
, computer_hand
instead of comp
)from random import choice as rc
def printCardNames(hand):
for i in hand:
name = ""
if(i < 13):
name += "Spades "
elif(i < 26):
name += "Clubs "
elif(i < 39):
name += "Hearts "
else:
name += "Diamonds "
cardName = i % 13
if(cardName == 0):
name += "2,"
elif(cardName == 1):
name += "3,"
elif(cardName == 2):
name += "4,"
elif(cardName == 3):
name += "5,"
if(cardName == 4):
name += "6,"
if(cardName == 5):
name += "7,"
if(cardName == 6):
name += "8,"
if(cardName == 7):
name += "9,"
if(cardName == 8):
name += "10,"
if(cardName == 9):
name += "J,"
if(cardName == 10):
name += "Q,"
if(cardName == 11):
name += "K,"
if(cardName == 12):
name += "A,"
print(name)
def total(hand):
values = [] # I will use value of the cards to calculate total value
for i in hand:
values.append(card_values[i])
aces = values.count(11)
t = sum(values)
if t > 21 and aces > 0:
while aces > 0 and t > 21:
t -= 10
aces -= 1
return t
cards = range(0,52) # I will pick a card, not a card value
card_values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, # corresponding card values
2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
cwin = 0
pwin = 0
while True:
player = []
player.append(rc(cards))
player.append(rc(cards))
pbust = False
cbust = False
while True:
tp = total(player)
print ("The player has these cards:")
printCardNames(player)
print ("Total Value of:", tp)
if tp > 21:
print ("--> The player is busted!")
pbust = True
break
elif tp == 21:
print ("\a BLACKJACK!!!")
break
else:
hs = input("Hit or Stand/Done (h or s): ").lower()
if 'h' in hs:
player.append(rc(cards))
else:
break
while True:
comp = []
comp.append(rc(cards))
comp.append(rc(cards))
while True:
tc = total(comp)
if tc < 18:
comp.append(rc(cards))
else:
break
print ("the computer has")
printCardNames(comp)
print ("Total Value of:", tc)
if tc > 21:
print ("--> The computer is busted!")
cbust = True
if pbust == False:
print ("The player wins!")
pwin += 1
elif tc > tp:
print ("The computer wins!")
cwin += 1
elif tc == tp:
print ("It's a draw!")
elif tp > tc:
if pbust == False:
print ("The player wins!")
pwin += 1
elif cbust == False:
print ("The computer wins!")
cwin += 1
break
print ("Wins, player = %d computer = %d" % (pwin, cwin))
exit = input("Press Enter (q to quit): ").lower()
if 'q' in exit:
break
print ("Thanks for playing blackjack with the computer!")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.