我正在尝试使用python构建BlackJack的应用程序。我在代码中遇到了keyerror的问题:>在Python中即将到来。 我的代码是
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}
flag=False
# 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):
c=list()
self.h=c # create Hand object
def __str__(self):
p=" "
for i in xrange(len(self.h)):
p+=str(self.h[i])
return p
# return a string representation of a hand
def add_card(self, card):
self.h.append(card) # add a card object to a hand
def get_value(self):
global flag
last=0
sum=0
# count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust
for i in xrange(len(self.h)):
last+=1
sum+=VALUES[str(self.h[i])[-1]]
if(str(self.h[i])[-1]=='A'):
flag=True
else:
flag=False
if(flag):
if(sum<=21):
sum+=10
else:
sum+=1
else:
if(sum>21):
if(last == len(self.h)):
return sum
#else:
#sum=sum-VALUES[str(self.h[i])[-1]]
# compute the value of the hand, see Blackjack video
def draw(self, canvas, pos):
# draw a hand on the canvas, use the draw method for cards
for i in xrange(len(self.h)):
card=Card(str(self.h[i]),VALUES[str(self.h[i])[-1]])
card.draw(canvas,[10+2*i,20])
# define deck class
class Deck:
def __init__(self):
deck=[]
# create a Deck object
for i in xrange(len(SUITS)):
for j in xrange(len(RANKS)):
k=Card(SUITS[i],RANKS[j])
deck.append(k)
self.d=deck
def shuffle(self):
# shuffle the deck
random.shuffle(self.d) # use random.shuffle()
def deal_card(self):
return random.choice(self.d)# deal a card object from the deck
def __str__(self):
o=" " # return a string representing the deck
for i in xrange(len(self.d)):
o+=str(self.d[i])
return o
#define event handlers for buttons
def deal():
global outcome, in_play
d1=Deck()
# your code goes here
d1.shuffle()
for i in xrange(4):
player.add_card(d1.deal_card)
dealer.add_card(d1.deal_card)
in_play = True
def hit():
# replace with your code below
global outcome,score,in_play
if(player.get_value()<=21):
player.add_card(d1.deal_card)
# if the hand is in play, hit the player
else:
outcome+="You are busted"
in_play=False
score=score-1
# if busted, assign a message to outcome, update in_play and score
def stand():
# replace with your code below
global outcome,in_play,score
if(not(in_play)): # if hand is in play, repeatedly hit dealer until his hand has value 17 or more
outcome=" "
outcome+="You are busted"
else:
while(dealer.get_value<=17):
dealer.add_card(d1.deal_card)
if(dealer.get_value>17):
outcome=" "
outcome+="The dealer busted"
else:
if(dealer.get_value>player.get_value):
outcome=" "
outcome+="Dealer wins"
score=score-1
else:
outcome=" "
outcome+="Player wins"
score=score+1
in_play=True
# 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])
canvas.draw_text(outcome,[150,100],20,"Blue")
canvas.draw_text(str(score),[400,100],20,"Blue")
# 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)
player=Hand()
dealer=Hand()
# get things rolling
deal()
frame.start()
在get_value函数中,我收到了Keyerror的错误:&gt;在第72行的python中。请帮忙。