0

I am trying to automatically create the following list:

[Slot('1A', '0', 0), Slot('2A', '0', 0),
 Slot('1B', '0', 0], Slot ('2B,'0', 0), ....]

By defining slot as:

class Slot:
    def __init__(self, address , card, stat):
        self.address = address
        self.card = card
        self.stat = stat


board = []
for i in range(1, 13):
    for j in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I']:
        board.append(Slot ((str(i)+j,'0', 0)))
print(board)

Using Python 3.5 in Windows. What is wrong? How I can do that? Thanks.

3
  • So what's wrong :-)? What's the output and what's wrong with it? Commented Sep 28, 2016 at 9:53
  • I'm quite inclined to edit the TypeError in there but not entirely sure it's a proper action to take. Commented Sep 28, 2016 at 10:55
  • Jim answer was very helpful. Thanks. Commented Sep 28, 2016 at 14:37

2 Answers 2

2

You have enclosed all arguments to Slot in a single parenthesis thereby passing a single argument to an __init__ that expects three (and the TypeError raised hints to that). Remove the unnecessary set of parenthesis:

board.append(Slot(str(i)+j,'0', 0))

and it works fine.

As an addendum, print(board) will return a quite unpleasant view of the objects, I'd suggest overloading __str__ and __repr__ to get a better view of the created objects:

class Slot:
    def __init__(self, address , card, stat):
        self.address = address
        self.card = card
        self.stat = stat

    def __str__(self):
        return "Slot: ({0}, {1}, {2})".format(self.address, self.card, self.stat)

    def __repr__(self):
        return str(self)

Now print(board) prints:

print(board)
[Slot: (1A, 0, 0), Slot: (1B, 0, 0),..., Slot: (12H, 0, 0), Slot: (12I, 0, 0)]
Sign up to request clarification or add additional context in comments.

Comments

0

You are passing a single tuple to the constructor. If you remove the parentheses, your code is golden.

From:
board.append(Slot ((str(i)+j,'0', 0)))
To:
board.append(Slot(str(i)+j,'0', 0))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.