我正在 CodeSkulptor 上开发 Python 初学者项目,以构建游戏 21 点。在我的 Blackjack 代码中,当我遍历 Hit 处理程序时收到此错误:AttributeError: 'str' object has no attribute 'get_rank'。这是指什么“str”对象?
本节出现错误,在Hand类中找到,get_value属性出现在这一行:for card in self.hand: 值 += VALUES[card.get_rank()]
# Mini-project #6 - Blackjack
#Jolen Martinez
import simplegui
import random
# load card sprite - 936x384 - source: jfitz.com
CARD_SIZE = (72, 96)
CARD_CENTER = (36, 48)
card_images = simplegui.load_image("http://storage.googleapis.com/codeskulptor-assets/cards_jfitz.png")
CARD_BACK_SIZE = (72, 96)
CARD_BACK_CENTER = (36, 48)
card_back = simplegui.load_image("http://storage.googleapis.com/codeskulptor-assets/card_jfitz_back.png")
# initialize some useful global variables
in_play = False
outcome = ""
score = 0
# define globals for cards
SUITS = ('C', 'S', 'H', 'D')
RANKS = ('A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K')
VALUES = {'A':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}
DECK = []
PLAYER_HAND = []
DEALER_HAND = []
# define card class
class Card:
def __init__(self, suit, rank):
if (suit in SUITS) and (rank in RANKS):
self.suit = suit
self.rank = rank
else:
self.suit = None
self.rank = None
print "Invalid card: ", suit, rank
def __str__(self):
return self.suit + self.rank
def get_suit(self):
return self.suit
def get_rank(self):
return self.rank
def draw(self, canvas, pos):
card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank),
CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit))
canvas.draw_image(card_images, card_loc, CARD_SIZE, [pos[0] + CARD_CENTER[0], pos[1] + CARD_CENTER[1]], CARD_SIZE)
# define hand class
class Hand:
def __init__(self):
self.hand = []
# create Hand object
def __str__(self):
handstr = 'Hand contains: '
for x in self.hand:
handstr = handstr + str(x) + ' '
return handstr # return a string representation of a hand
def add_card(self, card):
self.hand.append(card)
# add a card object to a hand
def get_value(self):
# count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust
value = 0
**for card in self.hand:
value += VALUES[card.get_rank()]**
for card in self.hand:
if card.get_rank == "A":
if value + 10 <= 21:
return value + 10
return value
def draw(self, canvas, pos):
pass # draw a hand on the canvas, use the draw method for cards
# define deck class
class Deck:
def __init__(self):
self.deck = ['CA', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'CT', 'CJ', 'CQ', 'CK',
'SA', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'ST', 'SJ', 'SQ', 'SK',
'HA', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'HT', 'HJ', 'HQ', 'HK',
'DA', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'DT', 'DJ', 'DQ', 'DK']
def shuffle(self):
# shuffle the deck
return random.shuffle(self.deck)
def deal_card(self):
return self.deck.pop()
def __str__(self):
deckstr = 'Deck contains: '
for x in self.deck:
deckstr = deckstr + str(x) + " "
return deckstr # return a string representation of a hand
# return a string representing the deck
#define event handlers for buttons
def deal():
global outcome, in_play, DECK, PLAYER_HAND, DEALER_HAND
DECK = Deck()
DECK.shuffle()
print str(DECK)
PLAYER_HAND = Hand()
DEALER_HAND = Hand()
PLAYER_HAND.add_card(DECK.deal_card())
PLAYER_HAND.add_card(DECK.deal_card())
DEALER_HAND.add_card(DECK.deal_card())
DEALER_HAND.add_card(DECK.deal_card())
print str(PLAYER_HAND)
print str(DEALER_HAND)
in_play = True
def hit():
global in_play
if in_play:
if PLAYER_HAND.get_value() <= 21:
PLAYER_HAND.add_card(DECK.deal_card())
if PLAYER_HAND.get_value() > 21:
in_play = False
# if the hand is in play, hit the player
# if busted, assign a message to outcome, update in_play and score
def stand():
global in_play
if in_play:
while DEALER_HAND.get_value() <= 17:
DEALER_HAND.add_card(DECK.deal_card())
in_play = False
if DEALER_HAND.get_value() > 21:
print "Dealer busted, Player Wins!"
elif DEALER_HAND.get_value() >= PLAYER_HAND.get_value:
print "Player Loses"
else:
print "Player Wins!"
# if hand is in play, repeatedly hit dealer until his hand has value 17 or more
# assign a message to outcome, update in_play and score
# draw handler
def draw(canvas):
# test to make sure that card.draw works, replace with your code below
card = Card("S", "A")
card.draw(canvas, [300, 300])
# initialization frame
frame = simplegui.create_frame("Blackjack", 600, 600)
frame.set_canvas_background("Green")
#create buttons and canvas callback
frame.add_button("Deal", deal, 200)
frame.add_button("Hit", hit, 200)
frame.add_button("Stand", stand, 200)
frame.set_draw_handler(draw)
# get things rolling
deal()
frame.start()
答案 0 :(得分:0)
你的 deal_card()
函数返回一个字符串,从
self.deck = ['CA', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'CT', 'CJ', 'CQ', 'CK',
'SA', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'ST', 'SJ', 'SQ', 'SK',
'HA', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'HT', 'HJ', 'HQ', 'HK',
'DA', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'DT', 'DJ', 'DQ', 'DK']
你需要做这样的事情:
def deal_card(self):
suit, rank = self.deck.pop()
return Card(suit, rank)
从deck
中获取一个字符串,分成一个花色字符和一个等级字符,用这些参数构建一个Card
对象,然后返回这个Card
对象。< /p>
你的代码与我的改编: https://py2.codeskulptor.org/#user48_nw49jEMU2m_0.py