簡體   English   中英

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() ,但我一直turtle.done()相同的問題。

這是我的代碼:

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()語句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是否對您更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM