0
我編寫了以下程序與用戶一起玩二十一點,但每當玩家獲得Jack,Queen或King時,total_value函數中的if語句都不會檢測到它們。我應該怎麼做才能解決這個問題?另外,你是否有任何一般指針來清理我的代碼或使我的語法更好?Python中的二十一點程序中未檢測到變量
import random
from random import randint
class deck():
"""This class holds the deck information"""
clubs = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
spades = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
hearts = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
diamonds = ["A", 2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K"]
suites = ["clubs", "spades", "hearts", "diamonds"]
def drawcard(self):
#This method removes a card from the deck and returns it.
#Do not draw cards when there are none left.
if self.suites:
suite = random.choice(self.suites)
suite_number = randint(0, len(self.suites) - 1)
if suite == "clubs":
card = (self.clubs).pop(randint(0, len(self.clubs) - 1))
if not self.clubs:
(self.suites).remove("clubs")
elif suite == "spades":
card = (self.spades).pop(randint(0, len(self.spades) - 1))
if not self.spades:
(self.suites).remove("spades")
elif suite == "hearts":
card = (self.hearts).pop(randint(0, len(self.hearts) - 1))
if not self.hearts:
(self.suites).remove("hearts")
elif suite == "diamonds":
card = (self.diamonds).pop(randint(0, len(self.diamonds) - 1))
if not self.diamonds:
(self.suites).remove("diamonds")
return card, suite
else:
return "OUT", "CARDS ERROR"
def total_value(hand):
#This function returns the current total value of a player's hand.
#For example, ["A", "K"] would return 21.
value = 0
aces = 0
for card in hand:
if card == "A":
aces = aces + 1
elif card == "J":
value == value + 10
elif card == "Q":
value == value + 10
elif card == "K":
value == value + 10
else:
value = value + card
if aces == 1:
if value <= 10:
value = value + 11
elif value > 10:
value = value + 1
elif aces == 2:
if value <= 9:
value = value + 12
elif value > 9:
value = value + 2
elif aces == 3:
if value <= 8:
value = value + 13
elif value > 8:
value = value + 3
elif aces == 4:
if value <= 7:
value = value + 14
elif value > 7:
value = value + 4
return value
new_deck = deck()
player_hand = [ ]
card1 = new_deck.drawcard()
card2 = new_deck.drawcard()
print "You have drawn a " + str(card1[0]) + " of " + card1[1] + " and a " + str(card2[0]) + " of " + card2[1] + "!"
player_hand.append(card1[0])
player_hand.append(card2[0])
dealer_hand = [ ]
card3 = new_deck.drawcard()
card4 = new_deck.drawcard()
dealer_hand.append(card3[0])
dealer_hand.append(card4[0])
gameover = False
win = False
dealer_finished = False
player_finished = False
while not gameover:
while dealer_finished == False:
if total_value(dealer_hand) < 17:
card = new_deck.drawcard()
dealer_hand.append(card[0])
elif total_value(dealer_hand) >= 17:
dealer_finished = True
if total_value(dealer_hand) > 21:
print "Dealer Busts!"
win = True
gameover = True
break
while player_finished == False:
choice = raw_input("Hit or Stay? ")
choice.capitalize()
if choice == "Hit":
card = new_deck.drawcard()
player_hand.append(card[0])
print "You drew a", card[0], "of " + card[1]
if total_value(player_hand) > 21:
player_finished = True
break
elif choice == "Stay":
player_finished = True
gameover = True
break
else:
print "Invalid Option"
if total_value(player_hand) > 21:
win == False
gameover == True
break
gameover == True
if win == True:
print "Congratulations!"
else:
print total_value(player_hand), total_value(dealer_hand)
if total_value(player_hand) > 21:
print "You bust!"
elif total_value(dealer_hand) > total_value(player_hand):
print "The dealer had", str(total_value(dealer_hand)), "but you only had", str(total_value(player_hand)) + "."
print "You lose!"
elif total_value(dealer_hand) == total_value(player_hand):
print "You tie the dealer with", total_value(dealer_hand)
elif total_value(dealer_hand) < total_value(player_hand):
print "The dealer had", str(total_value(dealer_hand)), "and you had", str(total_value(player_hand)) + ", so you win!"
哦,謝謝,我錯過了。 – Taylor 2013-03-18 22:39:59