Python 3.2.1:类型错误,参数数量等
我在网上查找我遇到的问题,但看到的各种说法对我帮助不大(我对Python还比较陌生,正在大学上编程入门课程)。
问题是:我总是收到一个关于参数数量的类型错误,但我(凭借我目前有限的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)
1 个回答
5
很难判断,因为你提供了很多代码,但我觉得事情是这样的:
wP = [Point(bX - (10 * bS) + 5, bS + 80), Point(bX - (10 * bS) + 105, bS + 120)]
这让 wP
成为一个包含两个元素的列表。尽管它里面有两个东西,但它仍然是一个整体。
goWest = Rectangle(wP)
我猜 Rectangle
的构造函数应该接收两个点作为参数。再加上一个隐含的 self
参数,它总是指向对象本身,这样总共就需要三个参数。Python 报错是因为它只看到了两个参数,也就是隐含的 self
和你的列表 wP
。
如果这真的是问题所在,解决这个问题的一种方法是:
goWest = Rectangle(wP[0], wP[1])
还有一种稍微高级一点的方法是:
goWest = Rectangle(*wP)
*
(参数解包)运算符基本上是自动将列表中的元素展开成单独的参数。f(*args)
相当于:
f(args[0], args[1], args[2], ...)