Python:Turtle 模块不会 运行 我在 Python 3.6 上的代码

Python: Turtle module won't run my code on Python 3.6

我正在做一些练习题,我在其中编写代码以便 turtle 可以绘制正方形、圆形、多边形等。问题是当我尝试 运行 我的代码时,turtle 是:

  1. 没有响应,我必须强制关闭它
  2. 只有 运行 我的代码中绘制正方形的部分

我在 Spyder 上使用 Python 3.6,并尝试在每个部分的末尾使用 turtle.mainloop()turtle.done(),但我将 运行ning 保留在同一部分问题。

这是我的代码:

import turtle
bob = turtle.Turtle()
print(bob)

bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
turtle.done()


for i in range(4):
    print("Hello!")

for i in range(4):
    bob.fd(100)
    bob.lt(90)
turtle.done()

t = turtle.Turtle()
def square(t):
    print(t)
    t.fd(100)
    t.lt(90)
    t.fd(100)
    t.lt(90)
    t.fd(100)
    t.lt(90)
    t.fd(100)
    t.lt(90)
turtle.done()

square(bob)
turtle.done()

t = turtle.Turtle()
def square(t):
    print(t)
    for i in range(4):
        t.fd(100)
        t.lt(90)
turtle.mainloop()
turtle.done()


t = turtle.Turtle()
def square(t, length):
    print(t)
    for i in range(4):
        t.fd(length)
        t.lt(90)

square(t, 200)

turtle.done()


t = turtle.Turtle()
def polygon(t, length, n):
    print(t)
    for i in range(4):
        t.fd(length)
        t.lt(360/n)

polygon(t, t = 200, n = 12)
turtle.done()

import math

def circle(t, r):
    circumference = 2 * math.pi * r
    n = 100
    length = circumference / n
    polygon(t, length, n)

circle(t, 100)
turtle.done()
"""draws a circle in turtle"""

你告诉计算机如何做事,但你从未真正告诉它去做。 运行 circle(t) 等来做到这一点。

你并没有始终如一地运行你想要的一切。仔细阅读您的代码并确保您始终 运行 mainloop 等..

多个 turtle.done() 语句,而实际上应该只有一个,而且不同的代码片段没有考虑其他代码片段绘制的位置,这使得它看起来像应该是单个文件中单个程序的集合:

计划 1:

import turtle

bob = turtle.Turtle()
print(bob)

bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)
bob.lt(90)
bob.fd(100)

for i in range(4):
    print("Hello!")

turtle.done()

计划 2:

import turtle

bob = turtle.Turtle()

for i in range(4):
    bob.fd(100)
    bob.lt(90)

turtle.done()

计划 3:

import turtle

def square(t):
    print(t)
    t.fd(100)
    t.lt(90)
    t.fd(100)
    t.lt(90)
    t.fd(100)
    t.lt(90)
    t.fd(100)
    t.lt(90)

bob = turtle.Turtle()

square(bob)

turtle.done()

计划 4:

import turtle

def square(t):
    print(t)
    for i in range(4):
        t.fd(100)
        t.lt(90)

t = turtle.Turtle()

square(t)

turtle.mainloop()

计划 5:

import turtle

def square(t, length):
    print(t)
    for i in range(4):
        t.fd(length)
        t.lt(90)

t = turtle.Turtle()

square(t, 200)

turtle.done()

计划 6:

import turtle
import math

def polygon(t, length, n):
    print(t)
    for i in range(n):
        t.fd(length)
        t.lt(360 / n)

t = turtle.Turtle()
polygon(t, length=50, n=12)

def circle(t, r):
    """draws a circle in turtle"""
    circumference = 2 * math.pi * r
    n = 100
    length = circumference / n
    polygon(t, length, n)

circle(t, 100)

turtle.done()

尝试 运行 将这些作为单独的程序放在单独的文件中,看看 turtle 是否更适合您。