我已经搜索了我遇到的问题,但是看到了它的不同变化对我没什么帮助(我对Python很新;把它作为大学编程入门的一部分)。< / p>
问题:我不断得到一个关于参数数量的TypeError,但是我(迄今为止我对Python的了解有限)看不出有什么问题。
我有以下功能:
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
和
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
我收到以下错误:
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)
答案 0 :(得分:5)
很难说,因为你提供了这么多代码,但我相信发生了什么:
wP = [Point(bX - (10 * bS) + 5, bS + 80), Point(bX - (10 * bS) + 105, bS + 120)]
这使wP
成为两个元素的列表。即使它包含两件事,它仍然只是一个对象。
goWest = Rectangle(wP)
我猜测Rectangle
构造函数应该作为参数传递两个点。除了始终引用对象本身的隐式self
参数外,它总共有3个参数。 Python抱怨说它只传递了两个参数,即隐式self
和你的列表wP
。
如果这实际上是问题,解决这个问题的一种方法是
goWest = Rectangle(wP[0], wP[1])
稍微更高级的方式是
goWest = Rectangle(*wP)
*
(参数解包)运算符基本上自动化将列表元素扩展为单个参数的过程。 f(*args)
相当于
f(args[0], args[1], args[2], ...)