这是我的代码,当我运行此代码时:
from turtle import Turtle
def draw_square():
window = turtle.Screen()
window.bgcolor("red")
brad = turtle.Turtle()
brad.shape("turtle")
brad.color("yellow")
brad.speed(2)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
什么也没有发生,只是外壳出现,并显示RESTART,就像您在图片中看到的那样。请帮帮我!
答案 0 :(得分:0)
请写下代码,而不要链接屏幕截图。使事情变得容易:)
该代码位于函数定义内,但您并未在任何地方调用该函数。尝试在程序结尾处调用它:
from turtle import Turtle
def draw_square(): # function definition
window = turtle.Screen()
...
...
brad.right(90)
draw_square() # call the function
答案 1 :(得分:0)
您的import
代码存在问题:
from turtle import Turtle
它与您的用法不一致:
window = turtle.Screen()
brad = turtle.Turtle()
由于您仅从乌龟中导入了Turtle
,所以这两行都不起作用。您可以执行以下任一操作:
import turtle
window = turtle.Screen()
brad = turtle.Turtle()
或(最好是仅使用面向对象的乌龟):
from turtle import Screen, Turtle
window = Screen()
brad = Turtle()
您的import
的下一个问题是,它与Python Shell错误消息中引用的import
不一致:
import Turtle
ImportError: No module named Turtle
这可能意味着您正在查看的代码与您正在运行的代码不相同。现在,让我们尝试从您的代码中构建一个一致,完整的(Python 2,我假设)程序:
from turtle import Screen, Turtle, mainloop
def draw_square(turtle):
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
window = Screen()
window.bgcolor("red")
brad = Turtle()
brad.shape("turtle")
brad.color("yellow")
brad.speed(2)
draw_square(brad)
mainloop()