我正在制作这个二十一点游戏,但我不断收到此错误:
回溯(最近通话最近): 在第151行的文件“ Card.py”中 pHand.addCard(deck.deal())
addCard中的文件“ Card.py”,第62行 self.value + = values [card.number] AttributeError:“ builtin_function_or_method”对象没有属性“ number”
我试图更改变量名称等,但没有解决任何问题。 谁能帮帮我吗? 错误出现在第151行和第62行。
import random
colors = ("Diamonds", "Hearts", "Spades", "Clubs")
numbers = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')
values = {'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}
Playing = True
class EksamenBlackjack:
def __init__(self, color, number):
self.color = color
self.number = number
def __str__(self):
return self.number + " : " + self.color
class Deck:
def __init__(self):
self.deck = [] #empty array
for color in colors:
for number in numbers:
self.deck.append(EksamenBlackjack(color, number))
def __str__(self):
deck_c = '' #empty string
for card in self.deck:
deck_c += '\n ' + card.__str__() # tilføj til hver Card object's print String
return "Deck have: { " + deck_c
def shuffle(self):
random.shuffle(self.deck)
def deal(self):
card_one = self.deck.pop
return card_one
class PlayerHand:
def __init__(self):
self.cards = [] # start with an empty list
self.ace = 0 #tracker of attribute
self.value = 0
def aceFix(self):
while self.value > 21 and self.ace:
self.value -= 10
self.ace -= -1
def addCard (self, card):
self.cards.append(card)
self.value += values[card.number]
if card.number == 'Ace':
self.ace += 1
class Money:
def __init__(self):
self.total = 1000
self.bet = 0
def betWin(self):
self.total += self.bet
def BetLose(self):
self.total -= self.bet
def makeBet(tokens):
while True:
try:
tokens.bet = int(input('Type in your bet!'))
except ValueError:
print("Don't be silly, you can only bet with tokens")
else:
if tokens.bet > tokens.total:
print("you Dont got enought mate", tokens.total)
else:
break
def hit(deck, plhand):
plhand.addCard(deck.deal())
plhand.aceFix()
def StandorHit(deck, plhand):
global Playing
while True:
i = input("what you wanna do? Hit or Stand? type 'hit or 'stand'")
if i[0] == 'hit':
hit(deck, plhand)
elif i[0] == 'stand':
print("you stands. Dealers turn")
Playing = False
else:
print("Try again please.")
continue
break
def showCards(dealer, player):
print("\n Computers hand:")
print("!! Hidden card !!")
print("", dealer.cards[1])
print("\n Players hand is: " *player.cards, sep="\n")
def showAll(player, dealer):
print("\n dealers amount =" *dealer.cards, sep="\n")
print("Dealers amount = ", dealer.value)
def winForPlayer(player,dealer, tokens):
print("you've won!")
tokens.betWin()
def bustsPlayer(player, dealer, tokens):
print("you busted")
tokens.BetLose()
def bustsDealer(player, dealer, tokens):
print("bust from dealer!")
tokens.betWin()
def winForDealer(player, dealer, tokens):
print("dealer won!")
tokens.BetLose()
def tie():
print("you've tied!")
while True:
print("<=============================================>")
print("Welcome to this Blackjack game. Rules are simple")
print("Get close to 21. If you go over you'll lose\nDealer hits till 21 and aces will count as 1 or 11")
print("<=============================================>")
#shuffle between player and deal two cards
deck = Deck()
deck.shuffle()
pHand = PlayerHand()
pHand.addCard(deck.deal())
pHand.addCard(deck.deal())
dHand = PlayerHand()
dHand.addCard(deck.deal())
dHand.addCard(deck.deal())
#tokens
pTokens = Money()
makeBet(pTokens)
showCards(dHand, pHand)
while Playing:
StandorHit(deck,pHand)
showCards(dHand,pHand)
if pHand.value > 21:
bustsPlayer(pHand, dHand,pTokens)
break
if dHand.value > 21:
bustsDealer(pHand,dHand,pTokens)
elif dHand.value > pHand.value:
winForDealer(pHand,dHand,pTokens)
elif dHand.value < pHand.value:
winForPlayer(pHand, dHand, pTokens)
else:
tie(pHand, dHand)
print("your bag is worth of: ", pTokens.total + "tokens")
playAgain = input("Wanna play again? Enter 'y' or 'n' ")
if playAgain[0] == "y":
playAgain = True
continue
else:
print("Thanks for playing, come back again")
break
答案 0 :(得分:1)
在deal
类的Deck
方法中,您正在做
card_one = self.deck.pop
应该是
card_one = self.deck.pop()
第一个分配方法pop
的引用,而第二个则实际调用它,并从列表self.deck
中为您提供一个项目。