我试图从经销商手和玩家手中获得总价值,但是我似乎无法得到它,因为变量没有定义。我只是想比较分数,所以我告诉他们是否都击中了二十一点,谁输了或谁赢了
谢谢。
import playing_cards
import random
players_hand = []
dealers_hand = []
#Gives point values to the cards
def get_hand_total(hand):
pointCount = 0
for i in range(0, len(hand)):
if ("K" in hand[i] or "Q" in hand[i] or "J" in hand[i] or "T" in hand[i]):
pointCount += 10
#As aces can be 1 or 11, if the current pointCount + 11 is more than 21, it will add 1.
elif("A" in hand[i]):
if (pointCount + 11) > 21:
pointCount += 1
else:
pointCount += 11
else:
for num in range(1,10):
if str(num) in hand[i]:
pointCount += num
if len(hand) > 1:
if (("T" in hand[0] or "K" in hand[0-1] or "Q" in hand[0-1] or "J" in hand[0-1] or "T" in hand[0-1])):
pointCount = 21
return(pointCount)
#Figures out what suites the cards belong too
def display_hand(hand_text, hand):
character = ""
character = character.join(hand)
str_conc = ""
for i in range(1, len(character ), 2):
if("S" in character [i]):
str_conc = str_conc + str(character [i - 1]) + " of of Spades \ "
elif("H" in character [i]):
str_conc = str_conc + str(character [i - 1]) + " of Hearts \ "
elif("C" in character [i]):
str_conc = str_conc + str(character [i - 1]) + " of Clubs \ "
elif("D" in character [i]):
str_conc = str_conc + str(character [i - 1]) + " of Diamonds \ "
str_conc = str_conc[:-2]
print(hand_text,str(get_hand_total(hand)) + ":", str_conc)
#Dealers Cards
card = playing_cards.deal_one_card()
dealers_hand.append(card)
card = playing_cards.deal_one_card()
dealers_hand.append(card)
display_hand("Dealer\'s hand is", dealers_hand)
#Players Hand
card = playing_cards.deal_one_card()
players_hand.append(card)
card = playing_cards.deal_one_card()
players_hand.append(card)
display_hand("Player\'s hand is", players_hand)
#Who wins?
if dealers_hand == 21:
print('dealer wins')
以下是playing_cards文件:;
#
# playing_cards module - PSP Assignment 1, sp2, 2017.
# DO NOT MODIFY!
#
import random
# Deck of cards - first letter represents the face value and
# second letter represents the suit
deck = ['AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS',
'AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC']
# Playing deck in use
playing_deck = []
# Function to determine whether there are any cards left in the
# deck of playing cards
# Parameters: No parameters
# Returns: True if the deck is empty, False otherwise
def is_empty_deck():
# Check to see whether playing deck is empty
return len(playing_deck) == 0
# Function to rebuild and shuffle the deck
# Parameters: No parameters
# Returns: Nothing is returned from the function.
def reset_deck():
global playing_deck
# Create new playing deck
playing_deck = deck.copy()
# Shuffle deck
random.shuffle(playing_deck)
# Function to deal one card
# Parameters: No parameters
# Returns: A string (containing two characters) representing
# the card delt, i.e. '2H' meaning 2 of Hearts
def deal_one_card():
# Check to see whether there are any cards left
if is_empty_deck():
# Rebuild and shuffle deck
reset_deck()
# Return a card (string of two characters)
return playing_deck.pop(0)
答案 0 :(得分:0)
请分享完整的代码,如果没有它将无法帮助您,但我想指出您将'dealers_hand'声明为列表,因此您无法将其比作“dealers_hand == 21” 。你将不得不循环遍历列表的元素,然后对它求和,然后将总和分开。
答案 1 :(得分:0)
请参阅以下主文件更新。你有一个方法来检查手的结果。因此,运行该方法,然后相互检查总和以确定胜利者是可行的。
您已尝试检查dealer_hand == 21但是它是一个列表,并且该列表不包含该手的分数。您需要通过get_hand_score(手)传递手来确定结果。获得结果后,您可以计算并确定获胜者。 :)
import playing_cards
import random
players_hand = []
dealers_hand = []
#Gives point values to the cards
def get_hand_total(hand):
pointCount = 0
for i in range(0, len(hand)):
if ("K" in hand[i] or "Q" in hand[i] or "J" in hand[i] or "T" in hand[i]):
pointCount += 10
#As aces can be 1 or 11, if the current pointCount + 11 is more than 21, it will add 1.
elif("A" in hand[i]):
if (pointCount + 11) > 21:
pointCount += 1
else:
pointCount += 11
else:
for num in range(1,10):
if str(num) in hand[i]:
pointCount += num
if len(hand) > 1:
if (("T" in hand[0] or "K" in hand[0-1] or "Q" in hand[0-1] or "J" in hand[0-1] or "T" in hand[0-1])):
pointCount = 21
return(pointCount)
#Figures out what suites the cards belong too
def display_hand(hand_text, hand):
character = ""
character = character.join(hand)
str_conc = ""
for i in range(1, len(character ), 2):
if("S" in character [i]):
str_conc = str_conc + str(character [i - 1]) + " of of Spades \ "
elif("H" in character [i]):
str_conc = str_conc + str(character [i - 1]) + " of Hearts \ "
elif("C" in character [i]):
str_conc = str_conc + str(character [i - 1]) + " of Clubs \ "
elif("D" in character [i]):
str_conc = str_conc + str(character [i - 1]) + " of Diamonds \ "
str_conc = str_conc[:-2]
print(hand_text,str(get_hand_total(hand)) + ":", str_conc)
def winner(d, p):
dealerTotal = get_hand_total(d)
playerTotal = get_hand_total(p)
if dealerTotal > playerTotal and dealerTotal <= 21:
print("Dealer Wins!")
elif dealerTotal == playerTotal:
print("Draw!")
elif playerTotal <= 21:
print("Player Wins!")
#Dealers Cards
card = playing_cards.deal_one_card()
dealers_hand.append(card)
card = playing_cards.deal_one_card()
dealers_hand.append(card)
display_hand("Dealer\'s hand is", dealers_hand)
#Players Hand
card = playing_cards.deal_one_card()
players_hand.append(card)
card = playing_cards.deal_one_card()
players_hand.append(card)
display_hand("Player\'s hand is", players_hand)
# Who wins?
winner(dealers_hand, players_hand)