1

Hello i'm new to programming and I have a problem with Python. For a minesweeper I want to create a method in the class bomb in order to place bombs randomly in a array so that's what i wrote :

from random import randint

import numpy as np

a = np.zeros((9, 9), dtype="i")
print(a)

class bomb:
    def __init__(self, nb):
        self.nb = nb

    def init_bomb(self, nb):
        i = 0
        for i in range(0, nb):
            x = randint(0,8)
            y = randint(0,8)
            a[x,y] = 9


bomb.init_bomb(10)

print(a)

When i run the code i get this error :

TypeError: init_bomb() missing 1 required positional argument: 'nb'

I don't understand why this nb because i wrote the nb that i want in between the parenthesis :

bomb.init_bomb(10)
0

5 Answers 5

1

The error message is trying to tell you that the self argument is missing.

The normal way to call a class method is to create an instance, then invoke the method on that instance:

bombs = bomb(10)
bombs.init_bomb(10)

Conventionally, however, the __init__ method should do all the necessary setup (and conventionally, the name of the class shoud be capitalized):

class Bomb:
    def __init__(self, nb):
        self.nb = nb
        self.init_bomb()

    def init_bomb(self):
        # i = 0  # not necessary
        for i in range(0, self.nb):
            x = randint(0,8)
            y = randint(0,8)
            a[x,y] = 9

Notice also how init_bomb reads self.nb, so it only needs the self handle to access the nb attribute.

Now, you simply

bombs = Bomb(10)

(The global variable a should probably be a parameter, too.)

A class can define a @staticmethod which can and must be called without a self argument, but here, you probably want to manipulate a particular instance. (There's also @classmethod which defines a method whose first argument is the class itself.)

A more sensible design altogether would perhaps be class Field which you initialize with the size of the desired matrix and the number of bombs to place into it. It doesn't really make sense for Bomb to just have a number of bombs (in which case it should be plural Bombs anyway, shouldn't it?) and then promptly forget about where they are.

Sign up to request clarification or add additional context in comments.

Comments

0

You didn't initialise the bomb instance. Try this instead:

from random import randint

import numpy as np

a = np.zeros((9, 9), dtype="i")
print(a)

class bomb:
    def __init__(self, nb):
        self.nb = nb

    def init_bomb(self):
        i = 0
        for i in range(0, self.nb):
            x = randint(0,8)
            y = randint(0,8)
            a[x,y] = 9


new_bomb = bomb(10)
new_bomb.init_bomb()

print(a)

Comments

0

You should just call the initializer throught the class like this:

my_bomb = bomb(10)

remember to save it to a variable otherwise it will be lost.

Comments

0

You Need To Initialize The Bomb Instance First


import numpy as np

a = np.zeros((9, 9), dtype="i")
print(a)

class bomb:
    def __init__(self, nb):
        self.nb = nb

    def init_bomb(self):
        i = 0
        for i in range(0, self.nb):
            x = randint(0,8)
            y = randint(0,8)
            a[x,y] = 9


new_bomb = bomb(10)
new_bomb.init_bomb()

print(a)

Comments

0

you haven't called the initializer use a variable to initialize bomb class

import numpy as np

a = np.zeros((9, 9), dtype="i")
print(a)

class bomb:
   def __init__(self, nb):
    self.nb = nb

   def init_bomb(self):
    i = 0
    for i in range(0, self.nb):
        x = randint(0,8)
        y = randint(0,8)
        a[x,y] = 9


       new_bomb = bomb(10)
       new_bomb.init_bomb()

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.