-4

I'm sorry to be asking this since a lot has been written on the subject but I have a lot of difficulties working it into a specific project.

The goal is to create a MarblesBoard class that takes a set of numbers as inputs and will then play a game to put the numbers in order.

Here is my code:

class MarblesBoard:

    def _init_(self, numbers):
        self.board = []
        for i in numbers:
            board[i] = numbers[i]

    def switch():
        temp = board[0]
        board[0] = board[1]
        board[1] = temp


def main():
    board = MarblesBoard((3,4,5))
    print("I'm here")

So input the numbers (as a tuple), put them into an array, and then manipulate them like in the switch method.

But I can't print anything when I get to my main method.

Jivan
  • 21,522
  • 15
  • 80
  • 131
Ted
  • 1
  • 1

2 Answers2

1

1) First fix is _init_ function which has double leading and trailing underscores

def __init__(self, numbers):

2) You are trying insert numbers (a tuple) into an empty list using tuples each element as index position which will raise index error.

For example:-

>>> board = []
>>> numbers = (3,4,5)
>>> for i in numbers:
...     board[i] = numbers[i]
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
IndexError: tuple index out of range

So you have to assign your numbers argument to self.board.

self.board = numbers

3) If switch is your instance method, you have to pass self as a first argument.

def switch(self):
    temp = self.board[0]
    #Your logic

So after all fix this is how it shoud look.

class MarblesBoard:

    def __init__(self, numbers):
        self.board = numbers

    def switch(self):
        temp = self.board[0]
        # your logic

def main():
    board_obj = MarblesBoard((3,4,5))
    print("I'm here")
    print board_obj.board

main()

Using __name__ == '__main__' inplace of main function.

if __name__ == '__main__':
    board_obj = MarblesBoard((3,4,5))
    print("I'm here")
    print board_obj.board  
Community
  • 1
  • 1
Tanveer Alam
  • 5,185
  • 4
  • 22
  • 43
  • @Ted Please review the answer and let me know if need any change. And if you feel the answer is correct or helped you in anyway, then please raise and accept the answer by clicking the correct symbol. – Tanveer Alam Dec 31 '14 at 16:09
-1

There are quite a few issues with your code. I guess it's your first python class.

First of all, main should be outside of the class. init is with two _. When you refer to member variables you must always use self. "For i in numbers" loops on the numbers themselves, not their indices. You can't just assign foo[i] = bar in python when foo is an empty array (I think you can in other languages). When you create member methods on classes, they must always have self as their first argument.

class MarblesBoard:

    def __init__(self, numbers):
        self.board = []
        for i in numbers:
            self.board.append(i)


    def switch(self):
        # Fixing this left as an exercise for the OP


if __name__=="__main__":
        board = MarblesBoard((3,4,5))
        print("I'm here")
grasshopper
  • 3,988
  • 3
  • 23
  • 29