1

I'm trying to make a "Battleship" like program.

This is what I have so far.

class Start: 
    def play(rows, columns):
        for i in rows: 
            for j in columns: 
                print("O")
print("Testing")
rowinput = input("rows: ")
colinput = input("columns: ")
s = start()
s.play(rowinput, colinput)

This is the error code I am getting:

Traceback (most recent call last):
File "C:/Users/OfficeUser/Documents/battleship.py", line 12, in <module>
s.play(rowinput, colinput)
TypeError: play() takes 2 positional arguments but 3 were given

My question is: How do I implement a row and column generation based on user input via input()?

0

2 Answers 2

2

Methods of class instances take the instance of the class as first argument.

Add self to the definition of play. The class instance gets passed when you call the method, so you need to handle that in your method definition:

def play(self, rows, columns):
    #...

There are other bugs waiting to go off, like: you'll need to cast your inputs to int, then you would need to use range in your loops:

for i in range(rows): 
    for j in range(columns): 
        print("O")
Sign up to request clarification or add additional context in comments.

6 Comments

I got an output of just "O" when adding self.
based on the input gathered, lets say row = 6 and column = 6, you will get a grid of 6x6
Yes, kinda weird since I remember using self with other projects and it working perfectly fine.
This was good. I managed to get the desired input. But what are the i and j for? Are they placeholders for the for-loops?
Use them in the loop directly. Like print(i, j)
|
0

When you call s.play(rowinput, colinput), the object "s" is also passed (implicitly) to the function "play" (as it's first argument). That's what the error is saying that three arguments are being passed to function play, but the signature says that it accepts only two. Now in python the convention is to use the name "self" for the parameter for the class instance. So just add another parameter to the function, like so

def play(self, rows, columns):

1 Comment

I got an output of just "O" when adding self

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.