Reputation: 65
I have searched for the problem I am having, but have seen different variations of it that don't help me much (I am fairly new to Python; taking it as part of an introduction to programming at a university).
The Problem: I keep getting a TypeError talking about the number of arguments, but I (with my so far limited knowledge of Python) do not see what is wrong.
I have the following functions:
def Grid():
borderSet = 20
spaceSize = 50
totalSpaces = 10
boardX = borderSet + (totalSpaces * spaceSize) + (10 * borderSet)
boardY = borderSet + (totalSpaces * spaceSize) + borderSet
board = GraphWin("Hunt the Wumpus", boardX, boardY)
for r in range(totalSpaces):
for c in range(totalSpaces):
gridTile = Rectangle(Point(borderSet+ r*spaceSize, borderSet+c*spaceSize), Point(borderSet+(r+1)*spaceSize, borderSet+(c+1)*spaceSize))
gridTile.setWidth(2)
gridTile.draw(board)
gridTileText = Text(Point(((gridTile.getP1().getX() + gridTile.getP2().getX()) / 2), ((gridTile.getP1().getY() + gridTile.getP2().getY()) / 2)), False)
gridTileText.draw(board)
ctr = DrawCharacter(borderSet, spaceSize)
ctr.draw(board)
a, w, p, t = ChooseDifficulty(boardX, boardY)
Text(Point(boardX - (6 * borderSet), 15 * borderSet), ("Number of arrows:", a)).draw(board)
Text(Point(boardX - (6 * borderSet), 16 * borderSet), ("Number of wumpii:", w)).draw(board)
Text(Point(boardX - (6 * borderSet), 17 * borderSet), ("Number of pits:", p)).draw(board)
Text(Point(boardX - (6 * borderSet), 18 * borderSet), ("Number of treasures:", t)).draw(board)
gW, gWT, gE, gET = Controls(boardX, boardY, borderSet)
gW.draw(board)
gWT.draw(board)
gE.draw(board)
gET.draw(board)
#gN.draw(board)
#gNT.draw(board)
#gS.draw(board)
#gST.draw(board)
click = board.getMouse()
#moveN = rectIntersect(gN, click)
#moveS = rectIntersect(gS, click)
moveE = rectIntersect(gE, click)
moveW = rectIntersect(gW, click)
board.getMouse()
Text(Point(boardX / 2, boardY / 2), (moveE, moveW)).draw(board)
board.getMouse()
ch = MoveCharacter(ctr, moveE, moveW)
ch.draw(board)
board.getMouse()
board.close()
return boardX, boardY
and
def Controls(bX, bY, bS):
wP = [Point(bX - (10 * bS) + 5, bS + 80), Point(bX - (10 * bS) + 105, bS + 120)]
eP = [Point(bX - (10 * bS) + 120, bS + 80), Point(bX - (10 * bS) + 220, bS + 120)]
goWest = Rectangle(wP)
goWestText = Text(Point(bX - (10 * bS) + 55, bS + 100), "Turn Left")
goWestText.setStyle("bold")
goWest.setFill(color_rgb(190, 30, 10))
goWestText.setFill(color_rgb(255, 255, 255))
goEast = Rectangle(eP)
goEastText = Text(Point(bX - (10 * bS) + 145, bS + 100), "Turn Right")
goEastText.setStyle("bold")
goEast.setFill(color_rgb(10, 190, 30))
return goWest, goWestText, goEast, goEastText
and I get the following error:
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
Grid()
File "<pyshell#5>", line 29, in Grid
gW, gWT, gE, gET = Controls(boardX, boardY, borderSet)
File "<pyshell#11>", line 10, in Controls
goWest = Rectangle(wP)
TypeError: __init__() takes exactly 3 arguments (2 given)
Upvotes: 0
Views: 880
Reputation: 131710
It's hard to tell because you've provided so much code, but I believe what's going on is this:
wP = [Point(bX - (10 * bS) + 5, bS + 80), Point(bX - (10 * bS) + 105, bS + 120)]
This makes wP
a list of two elements. Even though it contains two things, it's still a single object.
goWest = Rectangle(wP)
I'm guessing that the Rectangle
constructor is supposed to be passed two points as arguments. Along with the implicit self
parameter that always refers to the object itself, that makes a total of 3 arguments. Python is complaining that it's only seeing two parameters passed, namely the implicit self
and your list wP
.
If this is in fact the problem, one way to fix this would be
goWest = Rectangle(wP[0], wP[1])
A slightly more advanced way would be
goWest = Rectangle(*wP)
The *
(argument unpacking) operator basically automates the process of expanding the list elements into individual arguments. f(*args)
is equivalent to
f(args[0], args[1], args[2], ...)
Upvotes: 5