0

我收到一个错误,无论我做什么,我都无法摆脱这段代码。

Traceback (most recent call last):
  File "C:\Users\Ian\Desktop\BlackJack.py", line 226, in <module>
    main()
  File "C:\Users\Ian\Desktop\BlackJack.py", line 225, in main
    blackjack.playgame()
  File "C:\Users\Ian\Desktop\BlackJack.py", line 145, in playgame
    self.firstround()
  File "C:\Users\Ian\Desktop\BlackJack.py", line 83, in firstround
    a=self.dealer.hand.append(DeckofCards.deal(DeckofCards.shuffledeck))
AttributeError: type object 'DeckofCards' has no attribute 'shuffledeck'

以下是我编写的实际代码(为了消除错误,我进行了很多更改,但无法弄清楚发生了什么)我真的只是希望有人略过这个并告诉我明显的错误,我相信有很多,我很抱歉,但我们的教授不教如何做事,然后只是希望我们知道如何做。

from random import*


class Card(object):
    def __init__(self,suit,number):
        self.suit=suit
        self.number=number

class DeckofCards(object):
    def __init__(self,deck):
        self.deck=deck
        self.shuffledeck=self.shuffle()
        #print(self.shuffledeck)
    def shuffle(self):
        #print('This is shuffle function')
        b=[]
        count=0
        while count<len(self.deck):
            a=randrange(0,len(self.deck))
            if a not in b:
                b.append(self.deck[a])
                count+=1
        return(b)

    def deal(self):
        if len(self.shuffledeck)>0:
            return(self.shuffledeck.pop(0))
        else:
            shuffle(self)
            return(self.shuffledeck.pop(0))
class Player(object):
    def __init__(self,name,hand,inout,money,score,bid):
        self.name=name
        self.hand=hand
        self.inout=inout
        self.money=money
        self.score=score
        self.bid=bid
    def __str__(self):
        x = self.name + ":\t"
        x += "Card(s):"
        for y in range(len(self.hand)):
            x +=self.hand[y].face + self.hand[y].suit + " "
        if (self.name != "dealer"):
            x += "\t Money: $" + str(self.money)
        return(x)

class Game(object):
    def __init__(self,deck, player):
        self.player=Player(player,[],True,100,0,0)
        self.dealer=Player("Dealer",[],True,100,0,0)
        self.deck=DeckofCards(deck)
        self.blackjack= False #self.blackjacksearch()
    def blackjacksearch(self):#this is where it says there is an error we moved this because she said it needed to be in this class to get the Getot function 
        if self.player.hand.gettot()==21:
            return True
        else:
            return False    
    def firstround(self):
        self.player.inout=True
        self.player.hand=[]
        self.dealer.hand=[]
        a=self.dealer.hand.append(DeckofCards.deal(DeckofCards.shuffledeck))
        print(a)
        playerbid=int(input('How much would you like to bet?'))
        self.player.bid=playerbid
    def playturn(self):
        while self.player.blackjack!=True or hit=='yes':
            print(self.player.hand)
            a=self.player.hand.append(deal())
            print('The card that you just drew is ' + str(a))
            print(gettot())
            hit=input('Would you like to hit? ')
            if hit=='yes':
                return(self.player.hand.append(deal()))
            else:
                return() #might need to change this
        if self.player.blackjack==True:
            print(self.player.name + " has blackjack ")
        if hit=='no':
            print (self.player.hand.gettot())
    def playdealer(self):
        while self.dealer.hand<17:
            self.dealer.hand.append(deal())
            dealerhand=self.dealer.hand.gettot() #confused
            print(dealerhand)
        if self.dealer.hand==21:
            self.dealer.blackhjack=True
        dealerhand1=self.dealer.hand.gettot()
        print(dealerhand1)

    def gettot(self,hand):
        total=0
        for x in self.hand:
            if x==Card('H','A'):
                b=total+x
                if b>21:
                    total+=1
                else:
                    total+=11
            if x==Card('D','A'):
                b=total+x
                if b>21:
                    total+=1
                else:
                    total+=11
            if x==Card('S','A'):
                b=total+x
                if b>21:
                    total+=1
                else:
                    total+=11
            if x==Card('C','A'):
                if b>21:
                    total+=1
                else:
                    total+=11
            else:
                total+=x
        return(total)

    def playgame(self):
        play = "yes"
        while (play.lower() == "yes"):
            self.firstround()
            self.playturn()
            if self.player.blackjack == True:
                print(self.player.name + " got BLACKJACK! ")
                self.player.money += self.player.bid * 1.5
                print (self.player.name + " now has " + str(self.player.money))
                print("\n")
                self.player.inout = False
            if self.player.score > 21:
                print(self.player.name + " lost with a tot of " + str(self.player.score))
                self.player.money -= self.player.bid
                print (self.player.name + " now has " + str(self.player.money))
                print ("\n\n")
                self.player.inout = False
            self.playdealer()
            if self.dealer.blackjack == True:
                print("Dealer got blackjack, dealer wins\n")
                self.player.money -= self.player.bid
                print("Round\n")
                print("\t",self.dealer)
                print("\t",self.player)
                print("\t Dealer has " + str(self.dealer.score) + ", " + self.player.name + " has " + str(self.player.score))
            elif self.player.inout == True:
                print("Round\n")
                print("\t",self.dealer)
                print("\t",self.player)
                print("\n\t Dealer has " + str(self.dealer.score) + ", " + self.player.name + " has " + str(self.player.score))
                if self.dealer.score > 21:
                    print("\t Dealer lost with a total of " + str(self.dealer.score))
                    self.player.money += self.player.bid
                    print(self.player.name + " now has " + str(self.player.money))
                elif self.player.score > self.dealer.score:
                    print("\t" +self.player.name + " won with a total of " + str(self.player.score))
                    self.player.money += self.player.bid
                    print("\t"+self.player.name + " now has " + str(self.player.money))
                else:
                    print("\t Dealer won with a total of " + str(self.dealer.score))
                    self.player.money -= self.player.bid
                    print("\t"+self.player.name + " now has " + str(self.player.money))
            else:
                print("Round")
                print("\t",self.dealer)
                print("\t",self.player)
                if self.player.blackjack == False:
                    print("\t "+ self.player.name + " lost" )
                else:
                    print("\t "+self.player.name + " Won!")

            if self.player.money <= 0:
                print(self.player.name + " out of money - out of game ")
                play = "no"
            else:
                play = input("\nAnother round? ")
                print("\n\n")
        print("\nGame over. ")
        print(self.player.name + " ended with " + str(self.player.money) + " dollars.\n")
        print("Thanks for playing.  Come back soon!")



ls= [Card('H','A'),Card('H','2'),Card('H','3'),Card('H','4'),Card('H','5'),Card('H','6'),Card('H','7'),Card('H','8'),Card('H','9'),Card('H','10'),
Card('H','J'),Card('H','Q'),Card('H','K'),
Card('S','A'),Card('S','2'),Card('S','3'),Card('S','4'),Card('S','5'),
Card('S','6'),Card('S','7'),Card('S','8'),Card('S','9'),Card('S','10'),
Card('S','J'),Card('S','Q'),Card('S','K'),
Card('C','A'),Card('C','2'),Card('C','3'),Card('C','4'),Card('C','5'),
Card('C','6'),Card('C','7'),Card('C','8'),Card('C','9'),Card('C','10'),
Card('C','J'),Card('C','Q'),Card('C','K'),
Card('D','A'),Card('D','2'),Card('D','3'),Card('D','4'),Card('D','5'),
Card('D','6'),Card('D','7'),Card('D','8'),Card('D','9'),Card('D','10'),
Card('D','J'),Card('D','Q'),Card('D','K')]

'''tom=Card('Heart','Queen')
print(tom.suit)
print(DeckofCards(ls))
print(ls.suit)'''

def main():
    x = input("Player's name? ")
    blackjack = Game(ls,x)
    blackjack.playgame()
main()
4

3 回答 3

3

这条线是一个问题:

a=self.dealer.hand.append(DeckofCards.deal(DeckofCards.shuffledeck))
  • append返回None(无论您附加什么),所以a将是None
  • DeckofCards指的是对象,而不是类的实例。您可能想使用self.deck而不是DeckofCards.

x==Card('S','A'):也不行。你还没有__eq__Card对象定义,所以比较不起作用(你总是会得到False)。

于 2013-05-08T20:14:11.767 回答
0

shuffledeck属性只存在于DeckOfCards, 更改DeckOfCards.shuffledeck为的实例DeckOfCards().shuffledeck

于 2013-05-08T20:13:22.817 回答
0
class Game(object):
    def __init__(self,deck, player):
        self.player=Player(player,[],True,100,0,0)
        self.dealer=Player("Dealer",[],True,100,0,0)
        self.deck=DeckofCards(deck)
        self.blackjack= False #self.blackjacksearch()
    def blackjacksearch(self):#this is where it says there is an error we moved this because she said it needed to be in this class to get the Getot function 
        if self.player.hand.gettot()==21:
            return True
        else:
            return False    
    def firstround(self):
        self.player.inout=True
        self.player.hand=[]
        self.dealer.hand=[]
        a=self.dealer.hand.append(DeckofCards.deal(DeckofCards.shuffledeck))

您使用的课​​程不正确。DeckofCards 从未创建过。

        a=self.dealer.hand.append(self.deck.deal(self.deck.shuffledeck))
于 2013-05-08T20:14:47.767 回答