在python中的类的麻烦

时间:2013-05-08 20:09:38

标签: python python-3.x

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

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()

3 个答案:

答案 0 :(得分:3)

这一行是一个问题:

a=self.dealer.hand.append(DeckofCards.deal(DeckofCards.shuffledeck))
  • append会返回None(无论您追加什么内容),因此a将为None
  • DeckofCards指的是 class 对象,而不是该类的实例。您可能希望使用self.deck代替DeckofCards

x==Card('S','A'):也不起作用。您尚未为__eq__对象定义Card,因此比较不起作用(您将始终获得False)。

答案 1 :(得分:0)

shuffledeck属性仅适用于DeckOfCards的实例,将DeckOfCards.shuffledeck更改为DeckOfCards().shuffledeck

答案 2 :(得分: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))