【问题标题】:Draw a square using for loops in python?在python中使用for循环画一个正方形?
【发布时间】:2018-08-08 19:20:39
【问题描述】:
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)

我对编码一无所知,当我运行上面的代码时,什么也没有发生。谁能帮帮我。

【问题讨论】:

  • 请修正您的缩进以与您所拥有的完全匹配,并将您的代码格式化为代码(突出显示,ctrl + k)。

标签: python turtle-graphics


【解决方案1】:

您拥有大部分正确的部分,并且大致按正确的顺序排列。我们只需要在缩进、用法和样式方面对您的代码进行微调:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多