Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 2.7 在Python 2.7 Idle下使用turtle时出错_Python 2.7_Turtle Graphics - Fatal编程技术网

Python 2.7 在Python 2.7 Idle下使用turtle时出错

Python 2.7 在Python 2.7 Idle下使用turtle时出错,python-2.7,turtle-graphics,Python 2.7,Turtle Graphics,为什么我的代码在turtle()中显示错误?我使用的是Python2.7.13 Idle。查询涉及使用turtle绘制正方形: import turtle def draw_square(): window=turtle.Screen() window.bgcolor("red") brad= turtle.Turtle() brad.shape("yellow") # move forward brad.speed(2)# turn pen right 90 degrees bra

为什么我的代码在turtle()中显示错误?我使用的是Python2.7.13 Idle。查询涉及使用turtle绘制正方形:

import turtle
def draw_square():
 window=turtle.Screen()
 window.bgcolor("red")
 brad= turtle.Turtle()
 brad.shape("yellow") # move forward
 brad.speed(2)# turn pen right 90 degrees
 brad.forward(100)
 brad.right(90)
 brad.forward(100)
 brad.right(90)
 brad.forward(100)
 brad.right(90)
 brad.forward(100)
 brad.right(90)
window.exitonclick()
draw_square()

你在程序中犯了几个错误。首先,
window.exitonclick()
没有正确缩进,因此您从函数外部引用了
draw\u square()
的局部变量。你那惊人的狭窄,一个空格的缩进很可能是这个问题的原因

下一个错误是brad.shape(“黄色”)因为黄色不是形状,而是颜色。另外,
draw_square()
中的注释似乎在错误的行上。有些人可能不认为这是一个错误,但我知道。 您的代码经过重新编写,以修复上述问题,并以更符合逻辑的方式对其进行布局,以便进行海龟式编程:

import turtle

def draw_square(a_turtle):
    a_turtle.forward(100) # move forward
    a_turtle.right(90) # turn pen right 90 degrees
    a_turtle.forward(100)
    a_turtle.right(90)
    a_turtle.forward(100)
    a_turtle.right(90)
    a_turtle.forward(100)
    a_turtle.right(90)

window = turtle.Screen()
window.bgcolor("red")

brad = turtle.Turtle()
brad.shape("turtle")
brad.color("yellow")
brad.speed("slow")

draw_square(brad)

turtle.mainloop()

请告诉我们错误