[英]Why does “turtle.pd” produce a syntax error in my Python code?
我試圖制作一種復雜的參數化繪圖儀,但這並不重要。 重要的是我的程序應該使用 Turtle 圖形繪制一個圓圈,當我放下筆時,“ turtle.pd()
”行出現語法錯誤。 我不知道是怎么回事。 你們能幫幫我嗎? 我的程序如下。
import turtle, math, cmath
def f(x): return math.e ** (1j * x) # Use Python code to define f(x) as the return value; don't forget the math and cmath modules are imported
precision = 25 # This program will draw points every (1 / precision) units
def draw(x):
value = f(x)
try:
turtle.xcor = value.real * 25 + 100
turtle.ycor = value.imag * 25 + 100
turtle.pd() # Syntax error here
turtle.forward(1)
turtle.pu()
draw(0)
num = 0
while True:
num += 1
draw(num)
draw(-num)
我會補充
except [errortype]:
pass
在try
塊之后。 將 [errortype] 替換為您希望通過 try 塊減少的錯誤。 我看不出該塊內可能會引發什么錯誤,您可能只是寫
turtle.xcor = value.real * 25 + 100
turtle.ycor = value.imag * 25 + 100
並一起刪除 try 塊。
除了@dguis 指出的缺少except
子句語法錯誤(+1)之外,我想知道您認為這些行在做什么:
turtle.xcor = value.real * 25 + 100
turtle.ycor = value.imag * 25 + 100
如果.xcor
和.ycor
是您自己在海龜實例上隱藏的屬性,那么沒關系。 如果你認為這會移動烏龜——那么不會。 如果目標是移動海龜,請嘗試:
turtle.setx(value.real * 25 + 100)
turtle.sety(value.imag * 25 + 100)
帶有額外調整的完整解決方案:
import turtle
import math
def f(x):
return math.e ** complex(0, x)
def draw(x):
value = f(x) * 25
turtle.setx(value.real + 100)
turtle.sety(value.imag + 100)
turtle.pendown()
turtle.forward(1)
turtle.penup()
turtle.penup()
num = 0
draw(num)
while True:
num += 1
draw(num)
draw(-num)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.