【问题标题】:TypeError: draw_row() takes exactly 5 arguments (6 given) on line 55TypeError: draw_row() 在第 55 行采用 5 个参数(给定 6 个)
【发布时间】:2020-05-14 14:00:15
【问题描述】:

我周五要交作业,但我有一个问题,我的老师没有回复我,所以请你帮帮我吗?

我刚在学校开始学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)

【问题讨论】:

    标签: python python-turtle


    【解决方案1】:

    错误信息很清楚,你定义了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_colorsquare2_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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 2019-10-06
      相关资源
      最近更新 更多