Python 3.2.1:TypeError,参数数量等

htm*_*l92 0 python arguments typeerror

我已经搜索了我遇到的问题,但是看到它的不同变化对我没什么帮助(我对Python很新;把它作为大学编程入门的一部分).

问题:我不断得到一个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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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)
Run Code Online (Sandbox Code Playgroud)

Dav*_*d Z 5

这很难说,因为你提供了这么多代码,但我相信这是怎么回事:

wP = [Point(bX - (10 * bS) + 5, bS + 80), Point(bX - (10 * bS) + 105, bS + 120)]
Run Code Online (Sandbox Code Playgroud)

wP列出了两个元素.即使它包含两件事,它仍然只是一个对象.

goWest = Rectangle(wP)
Run Code Online (Sandbox Code Playgroud)

我猜测Rectangle构造函数应该作为参数传递两个点.除了self始终引用对象本身的隐式参数外,它总共有3个参数.Python抱怨说它只传递了两个参数,即隐式self和你的列表wP.

如果这实际上是问题,解决这个问题的一种方法是

goWest = Rectangle(wP[0], wP[1])
Run Code Online (Sandbox Code Playgroud)

稍微先进一点的方法

goWest = Rectangle(*wP)
Run Code Online (Sandbox Code Playgroud)

*(参数拆包)操作基本上自动展开列表元素融入到各个参数的过程.f(*args)相当于

f(args[0], args[1], args[2], ...)
Run Code Online (Sandbox Code Playgroud)