0

I'm learning python for a few weeks now on udemy.com and for my OOP classes, the mentor asked us to create a Blackjack game. My first task was to create a class for the deck. And I made this:

class Deck(object):
    totalCards = 0
    deck = [
        ["A", totalCards],
        ["2", totalCards],
        ["3", totalCards],
        ["4", totalCards],
        ["5", totalCards],
        ["6", totalCards],
        ["7", totalCards],
        ["8", totalCards],
        ["9", totalCards],
        ["10", totalCards],
        ["J", totalCards],
        ["Q", totalCards],
        ["K", totalCards],
    ]

    def __init__(self, numberOfDecks):
        self.numberOfDecks = numberOfDecks
        Deck.totalCards = numberOfDecks * 4

    def printDeck():
        for i in Deck.deck:
            print i

newDeck = Deck(6)
newDeck.printDeck()

The thing is... when I try to print the deck, I get and error that tells me that the method printDeck takes no argument and I'm passing one. I have no idea why.. Can anyone help me?

  • Python 2.7.11
  • I'm using Windows 10
  • CMD
  • Sublime Text 3
sergiomafra
  • 1,174
  • 2
  • 13
  • 21
  • 5
    It's implicitly passing the object to the method, change the method's declaration to `def printDeck(self):`. – Maroun Mar 21 '16 at 14:28
  • Also, probably you should be doing `self.totalCards = numberOfDecks * 4` and `for i in self.deck:` instead of `Deck.totalCards = numberOfDecks * 4` and `for i in Deck.deck:`. In the first case you're modifying the deck/totalCards of the instance. The second one modifies the deck/totalCards of the class. This will affect you if you want to have multiple Deck instances – Mr. E Mar 21 '16 at 14:52
  • Related question: "[Why do you need explicitly have the “self” argument into a Python method?](https://stackoverflow.com/questions/68282/why-do-you-need-explicitly-have-the-self-argument-into-a-python-method)". – Kevin J. Chase Mar 21 '16 at 15:02

1 Answers1

2

self - that is, the object - is always passed to class methods as the first argument, but your method definition takes no arguments, not even self - see here for elaboration.

nthall
  • 2,847
  • 1
  • 28
  • 36