Reputation: 55
My program is supposed to draw a chessboard using user input. The drawing has to occur with the call of draw(). I am getting an error:
line 18, in main
chessboard = Chessboard(tr, startX, startY, eval(width), val(height))
TypeError: __init__() takes from 3 to 5 positional arguments but 6 were given
The thing is that my main module cannot change. I have to leave it as it is. It's a requirement. So, how can I solve that problem when I cannot change a thing from it? It only gives me an error on line 18.
main module:
import turtle
from chessboard import Chessboard
def main():
startX, startY = eval(input("Enter a starting point: "))
width = input("Input a width: ")
height = input("Input a height: ")
tr = turtle.Turtle()
if width == "" and height == "":
chessboard = Chessboard(tr, startX, startY)
elif height == "":
chessboard = Chessboard(tr, startX, startY, width=eval(width))
elif width == "":
chessboard = Chessboard(tr, startX, startY, height=eval(height))
else:
chessboard = Chessboard(tr, startX, startY, eval(width), eval(height))
chessboard.draw()
tr.hideturtle()
turtle.done()
main()
I leave my chessboard.py module in case it has to do something with it:
import turtle
class Chessboard:
def __init__(self, startX, startY, width=250, height=250):
self.__startX = startX
self.__startY = startY
self.__width = width
self.__height = height
def draw(self, __startX, __startY, __width=250, __height=250):
turtle.clear
self.__drawChessboard(__startX, __startY, __width, __height)
def __drawChessboard(self, __startX, __startY, __width=250, __height=250):
# uses height x width, default 250
turtle.setheading(0)
turtle.penup()
turtle.goto(__startX, __startY)
turtle.pendown()
turtle.goto(__startX + __width, __startY)
turtle.goto(__startX + __width, __startY + __height)
turtle.goto(__startX, __startY + __height)
turtle.goto(__startX, __startY)
self.__drawAllRectangles(self, __startX, __startY, __width, __height)
turtle.exitonclick()
def __drawAllRectangles(self, __startX, __startY, __width, __height):
__checkerWidth = __width / 8
__checkerHeight = __height / 8
for row in range(8):
for column in range(4):
if row % 2 == 0:
self.__drawRectangle(self, __startX + (2 * column * __checkerWidth),
__startY + (row * __checkerHeight), __checkerWidth, __checkerHeight)
else:
self.__drawRectangle(self, __startX + __checkerWidth + (2 * column * __checkerWidth),
__startY + (row * __checkerHeight), __checkerWidth, __checkerHeight)
def __drawRectangle(self, __startX, __startY, __checkerWidth, __checkerHeight):
turtle.penup()
turtle.setheading(0)
turtle.goto(__startX, __startY)
turtle.fillcolor("black")
turtle.begin_fill()
turtle.pendown()
turtle.goto(__startX + __checkerWidth, __startY)
turtle.goto(__startX + __checkerWidth, __startY + __checkerHeight)
turtle.goto(__startX, __startY + __checkerHeight)
turtle.goto(__startX, __startY)
turtle.end_fill()
Thanks for your help!
Upvotes: 1
Views: 8752
Reputation: 1550
The first argument of every class method, including init, is always a reference to the current instance of the class. By convention, this argument is always named self. In the init method, self refers to the newly created object; in other class methods, it refers to the instance whose method was called.
EDIT: I didnt read the part where you said you can't change the main module, I apologize
class Chessboard:
def __init__(self, tr, startX, startY, width=250, height=250):
self.tr = tr
self.__startX = startX
self.__startY = startY
self.__width = width
self.__height = height
def draw(self):
self.tr.clear()
self.__drawChessboard(self.__startX, self.__startY, self.__width, self.__height)
def __drawChessboard(self, __startX, __startY, __width=250, __height=250):
# uses height x width, default 250
self.tr.setheading(0)
self.tr.penup()
self.tr.goto(__startX, __startY)
self.tr.pendown()
self.tr.goto(__startX + __width, __startY)
self.tr.goto(__startX + __width, __startY + __height)
self.tr.goto(__startX, __startY + __height)
self.tr.goto(__startX, __startY)
self.__drawAllRectangles(__startX, __startY, __width, __height)
self.tr.exitonclick()
def __drawAllRectangles(self, __startX, __startY, __width, __height):
__checkerWidth = __width / 8
__checkerHeight = __height / 8
for row in range(8):
for column in range(4):
if row % 2 == 0:
self.__drawRectangle(__startX + (2 * column * __checkerWidth),
__startY + (row * __checkerHeight), __checkerWidth, __checkerHeight)
else:
self.__drawRectangle(__startX + __checkerWidth + (2 * column * __checkerWidth),
__startY + (row * __checkerHeight), __checkerWidth, __checkerHeight)
def __drawRectangle(self, __startX, __startY, __checkerWidth, __checkerHeight):
self.tr.penup()
self.tr.setheading(0)
self.tr.goto(__startX, __startY)
self.tr.fillcolor("black")
self.tr.begin_fill()
self.tr.pendown()
self.tr.goto(__startX + __checkerWidth, __startY)
self.tr.goto(__startX + __checkerWidth, __startY + __checkerHeight)
self.tr.goto(__startX, __startY + __checkerHeight)
self.tr.goto(__startX, __startY)
self.tr.end_fill()
Upvotes: 5