类型错误:leftMove() 缺少 1 个必需的位置参数:'lts',并且类型错误:rightMove() 缺少 1 个必需的位置参数:'rts'

时间:2021-01-08 05:36:53

标签: python

在这个程序中,我的代码中有十二只海龟(左边六只,右边六只)。我创建了两个函数,可以让用户通过指定的左海龟和右海龟的按键移动左海龟之一和右海龟之一。当我尝试在左侧移动我的第一只乌龟时,当我尝试在左侧移动第一只乌龟时,我收到一条错误消息,提示“TypeError: leftMove() missing 1 required positional argument: 'lts'”。我在尝试移动右侧的第一只乌龟时也收到此错误消息,当我还尝试移动右侧的第一只乌龟时,显示“TypeError: rightMove() missing 1 required positional argument: 'rts'”。如何解决此错误消息,以便用户可以控制第一只左右乌龟的移动?

这是我的代码:

import turtle as trtl

leftTurtles = [] #Stores the turtle from leftTurtleShapes    
rightTurtles = [] #Stores the turtle from rightTurtleShapes

leftTurtleShapes = ["triangle", "circle", "arrow", "square", "turtle", "classic"] 
rightTurtleShapes = ["classic", "square", "circle", "triangle", "arrow", "turtle"]
leftTurtleColors = ["gold", "silver", "dodgerblue", "greenyellow", "peru", "crimson"] #Color aligns with leftTurtleShapes
rightTurtleColors = ["dodgerblue", "peru", "gold", "crimson", "greenyellow", "silver"] #Color aligns with rightTurtleShapes

for d in leftTurtleShapes: #Makes the turtle of leftTurtleShapes and the color of leftTurtleColors to be aligned with leftTurtleShapes and be stored in leftTurtles
    lts = trtl.Turtle(shape=d)
    leftTurtles.append(lts)
    lts.penup()
    leftColor = leftTurtleColors.pop()
    lts.fillcolor(leftColor)
    lts.goto(-350, 0) #Moves the leftTurtle to the left side of the window
    lts.setheading(0)

for s in rightTurtleShapes: #Makes the turtle of rightTurtleShapes and the color of rightTurtleColors to be aligned with rightTurtleShapes and be stored in rightTurtles
    rts = trtl.Turtle(shape=s)
    rightTurtles.append(rts)
    rts.penup()
    rightColor = rightTurtleColors.pop()
    rts.fillcolor(rightColor)
    rts.goto(350, 0) #Moves the rightTurtle to the right side of the window
    rts.setheading(180)

def rightMove(rts): #User can control the movement of one of the right turtles
    rts.setheading(180)
    rts.forward(1)

def leftMove(lts): #User can control the movement of one of the left turtles
    lts.setheading(0)
    lts.forward(1)

rightMove(rightTurtles[0]) #User controls the first right turtle
leftMove(leftTurtles[0]) #User controls the first left turtle

wn = trtl.Screen()
wn.onkeypress(leftMove, "d") #User uses the letter "d" to control the movement of the left turtle
wn.onkeypress(rightMove, "e") #User uses the letter "e" to control the movement of the right turtle
wn.listen()
wn.mainloop()

1 个答案:

答案 0 :(得分:0)

doc 中,函数不能接受任何参数。

所以你可以重写为:

def rightMove(): #User can control the movement of one of the right turtles
    rts = ... # set your turtle here
    rts.setheading(180)
    rts.forward(1)

def leftMove(): #User can control the movement of one of the left turtles
    lts = ... # set your turtle here
    lts.setheading(0)
    lts.forward(1)