Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何开始重新编写Python作业代码?_Python_Turtle Graphics - Fatal编程技术网

如何开始重新编写Python作业代码?

如何开始重新编写Python作业代码?,python,turtle-graphics,Python,Turtle Graphics,这是我的作业: 编辑此代码以确保参数反映超级矩形(例如,行=6,正方形=60,超级矩形将有6行10个正方形) 代码如下: 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=

这是我的作业:

编辑此代码以确保参数反映超级矩形(例如,行=6,正方形=60,超级矩形将有6行10个正方形)

代码如下:

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到底在问什么。但是,运行代码时,很明显它是错误的,正如目标部分中提到的情况:

e、 g.行=6,正方形=60,那么你的超级矩形将有6个 10个正方形的一排

不起作用。当您调用
超级矩形(bob,6,60,30)
时,在修复代码缩进后,您会得到:

多次透支。我们可以把一个创可贴放在(和清理)OP的代码中,以解决这种情况:

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()

我真的需要画而不是冲压

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

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()

好的,objective#1说您应该能够指定正方形和行的数量,这听起来像
input()
。至于目标#3,您应该能够从行和正方形的输入中获得有多少行正方形。这听起来像是简单的除法。也许这对代码更好review@G.LC:不,我认为这个家庭作业的想法是代码不起作用,需要修复。反馈:Stack Overflow社区非常乐意帮助初学者,但这里看起来您甚至还没有开始这方面的工作。我们的经验是,没有一位教师会给学生布置一份无法完成的作业,因此你可能已经参加了相关的讲座,或者已经以某种方式获得了学习材料。最好的学习方法是经历真正尝试的艰辛,即使一开始这是一场斗争。得到一个解决方案(并提出同样的要求)意味着你不太可能在长期内内化预期的教育目标。我需要真正的画画,而不是盖章。@John_Kabir,我添加了第三种使用绘图的方法,它比原始方法更简单、更快。这是一个很好的答案,但我认为这是过早给出的。OP不太可能学到任何东西,因为他们在盘子里得到了一个广泛的答案,而且他们完全没有付出任何努力。我希望他们的导师能认可这项工作出人意料地出色,并为他们的学校援引学术诚信政策。@halfer,我并不完全不同意。删除[家庭作业]标签使这个问题成为我的灰色地带。海报上有90%的代码,所以我在最初的回答中指出需要一个状态变量。我不知道他们一开始用了多少代码。我教Python编程,我对发布的内容感到难过。断开是一回事,但不正确的缩进,没有空格,没有像样的代码注释,等等,更让我烦恼。书籍和教师教什么?我的目标是展示现实的代码,这样他们的下一个任务会更好。我会考虑你的评论。谢谢你,谢谢。我同意您对HW标签的评估;我可以理解为什么从纯粹的分类学角度删除它,但它确实很有用。
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()