在python中使用for循环绘制正方形?

时间:2018-08-08 19:20:40

标签: python turtle-graphics

import turtle

def Draw_turtle(my_turtle):
    for i in range(1,5):

        my_turtle.turtle.forward(100)
        my_turtle.turtle.right(90)


        window = turtle.Screen()
        window.bgcolor('green')



        Alex = turtle.Turtle()
        Alex.color('black')
        Alex.speed(2)
        Alex.shape('triangle')

        Draw_turtle(Alex)

我还不知道编码,当我运行上面的代码时什么也没发生。谁能帮我。

1 个答案:

答案 0 :(得分:0)

您拥有大部分正确的作品,而且顺序大致正确。我们只需要在缩进,用法和样式方面对您的代码进行一些微调:

import turtle

def draw_square(my_turtle):

    for i in range(4):
        my_turtle.forward(100)
        my_turtle.right(90)

window = turtle.Screen()
window.bgcolor('green')

alex = turtle.Turtle()
alex.color('black')
alex.speed('slow')
alex.shape('triangle')

draw_square(alex)

window.mainloop()