Tammy Logger
Tammy Logger

Reputation: 61

trouble with classes in python

I am getting an error that reads this and no matter what I do I am unable to get rid of this code.

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'

The following is the actual code that I have written ( I have changed a great deal in order to move the error along but can no longer figure out what is going on) I am really just want someone to skim over this and tell me about the obvious mistakes, I am sure there are many, and I apologize but our professor doesn't teach use how to do things and then just expects us to know how.

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

Upvotes: 0

Views: 158

Answers (3)

Blender
Blender

Reputation: 298582

This line right is one problem:

a=self.dealer.hand.append(DeckofCards.deal(DeckofCards.shuffledeck))
  • append returns None (regardless of what you append), so a will be None
  • DeckofCards refers to the class object, not an instance of the class. You probably want to use self.deck instead of DeckofCards.

x==Card('S','A'): isn't going to work either. You haven't defined __eq__ for the Card object, so comparisons don't work (you'll always get False).

Upvotes: 3

beiller
beiller

Reputation: 3135

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

You are using the class incorrectly. The DeckofCards was never created.

        a=self.dealer.hand.append(self.deck.deal(self.deck.shuffledeck))

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208705

The shuffledeck attribute only exists for instances of DeckOfCards, change DeckOfCards.shuffledeck to DeckOfCards().shuffledeck.

Upvotes: 0

Related Questions