1

我星期五有作业,但我有一个问题,我的老师没有回复我,所以你能帮我吗?

我刚在学校开始使用 python turtle,这是我写的代码

from turtle import*

def square(square_color, length):
  pd()
  color(square_color)
  begin_fill
  for i in range(4):
    fd(length)
    lt(90)
  end_fill()
  pu()
  fd(length)

speed(0)
n= int(input("Enter a number:"))
pu()
bk(n*25)
pu()
bk(n*25)

for cur_square in range(n):
  if cur_square % 2 ==0:
    square("black", 50)
  if cur_square % 2 ==1:
    square("red", 50)
  if cur_square % 2 ==2:
    square("gray", 50)

def draw_row(rows, length, square1_color, square2_color, cur_square):
  for i in range(rows):
    if(cur_square + i)% 2 ==0:
      square(square1_color, length)
    if (cur_square + i )%2 ==1:
      square(square2_color, length)


def move_up(rows, length):
  lt(90)
  fd(length)
  rt(90)
  bk(rows*length)


length = int(input("The length of one square: "))
rows = int(input("The total amount of rows:"))
color1 = input("Color of the first square:")
color2 = input("Color of the second square:")
color3 = input("Color of the third square:")

speed(0)
pu()
setpos(-rows/2 * length, -rows/2 * length)

for cur_square in range(rows):
  draw_row(rows, length, color1, color2, color3, cur_square)
  move_up(rows,length)

它告诉我我有一个错误 - TypeError: draw_row() 在第 55 行正好采用 5 个参数(给定 6 个)

我应该怎么办?

我有错误的确切行

for cur_square in range(rows):
      draw_row(rows, length, color1, color2, color3, cur_square)
      move_up(rows,length)
4

1 回答 1

2

错误消息很清楚,您定义draw_row()了五个参数:

def draw_row(rows, length, square1_color, square2_color, cur_square):

然后到了调用它的时候,你通过了六个:

draw_row(rows, length, color1, color2, color3, cur_square)

根据定义,draw_row()仅处理两种颜色,square1_color并且square2_color

def draw_row(rows, length, square1_color, square2_color, cur_square):
    for i in range(rows):
        if (cur_square + i) % 2 == 0:
            square(square1_color, length)
        else:
            square(square2_color, length)

但是当你调用它时,你传入了三种不同的颜色,color1color2color3

于 2020-05-14T20:43:31.753 回答