我周五有家庭作业,但是我有一个问题,我的老师没有回复我,所以你能帮我吗?
我刚刚在学校开始使用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)
错误消息很清楚,您定义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)
但是当您调用它时,您传递了color1
,color2
和color3
这三种不同的颜色。