我怎样才能开始重写这个 Python 作业代码?

How can I make a start on re-writing this Python homework code?

这是我的作业:

Edit this code to make sure the parameters reflect the super rectangle (for example rows = 6 and squares = 60, the super rectangle will have 6 rows of 10 squares.)

代码如下:

import turtle

import time

bob = turtle.Turtle()

def make_square(bob, length):

for x in range(4):

    bob.rt(90)
    bob.fd(length)


def super_rectangle(bob, rows=2, squares=4, length=100):

height = (length / rows)

columns = int(squares / rows)

for row in range(rows):
    for column in range(columns):
        bob.fd(length)
        make_square(bob, length)
    bob.rt(90)
    bob.fd(length * 2)
    bob.rt(90)
    time.sleep(1)

super_rectangle(bob, length=100)

我不清楚 OP 到底在问什么。然而,运行 代码,很明显它是错误的,如 objective 部分中提到的情况:

e.g. rows = 6 and squares = 60, then your super rectangle will have 6 rows of 10 squares.

无效。当您调用 super_rectangle(bob, 6, 60, 30) 时,修复代码缩进后,您会得到:

多次透支。我们可以在 OP 的代码上放置一个 band-aid(并清理)来解决这个问题:

from turtle import Screen, Turtle

def make_square(turtle, length):

    for _ in range(4):
        turtle.left(90)
        turtle.forward(length)

def super_rectangle(turtle, rows=2, squares=4, length=100):

    columns = squares // rows

    parity = 1

    for row in range(rows):
        for _ in range(columns):
            turtle.forward(length)
            make_square(bob, length)

        turtle.right(parity * 90)
        if parity < 1 and row < rows - 1:
            turtle.forward(length * 2)
        turtle.right(parity * 90)

        parity = 0 - parity

screen = Screen()

bob = Turtle()
bob.speed('fastest')  # because I have no patience

super_rectangle(bob, 6, 60, 30)

screen.exitonclick()

绘制描述的输出:

但如果我们按字面理解 OP 的标题:

Different way to re-write this python code?

那么我建议,而不是绘图,才是处理这个问题的正确方法。这种方法使代码更简单、更快:

from turtle import Screen, Turtle

CURSOR_SIZE = 20

def super_rectangle(turtle, rows=2, squares=4, length=100):

    columns = squares // rows

    turtle.shapesize(length / CURSOR_SIZE)

    parity = 1

    for _ in range(rows):
        for _ in range(columns):
            turtle.stamp()
            turtle.forward(parity * length)

        x, y = turtle.position()
        turtle.setposition(x + -parity * length, y + length)

        parity = 0 - parity

screen = Screen()

bob = Turtle('square', visible=False)
bob.color("black", "white")
bob.penup()

super_rectangle(bob, 6, 60, 30)

screen.exitonclick()

I need to really draw not stamping.

我们可以使用 绘图 以完全不同的方式实现这一点,这比您的修补代码更简单、更高效。关键是要解决图纸中的大量冗余问题。我们将画出所有水平线,然后画出所有垂直线,而不是绘制单个正方形:

from turtle import Screen, Turtle

def make_serpentine(turtle, length, rows, columns, parity=1):

    for _ in range(rows):
        turtle.forward(length * columns)
        turtle.left(parity * 90)
        turtle.forward(length)
        turtle.left(parity * 90)
        parity = 0 - parity

def super_rectangle(turtle, rows=2, squares=4, length=100):

    columns = squares // rows

    make_serpentine(turtle, length, rows, columns)

    turtle.forward(length * columns)
    turtle.right(90)

    make_serpentine(turtle, length, columns, rows, -1)  # reverse sense of rows & columns

    turtle.forward(length * rows)
    turtle.left(90)  # leave things as we found them

screen = Screen()

bob = Turtle()

super_rectangle(bob, 6, 60, 30)

screen.exitonclick()