[英]Why does “turtle.pd” produce a syntax error in my Python code?
I was trying to make a sort-of complex parametric grapher, but that isn't what's important.我试图制作一种复杂的参数化绘图仪,但这并不重要。 What's important is that my program is supposed to draw a circle using Turtle graphics, and when I put the pen down, I have a syntax error in the " turtle.pd()
" line.重要的是我的程序应该使用 Turtle 图形绘制一个圆圈,当我放下笔时,“ turtle.pd()
”行出现语法错误。 I have no idea what's going on.我不知道是怎么回事。 Can you guys help me?你们能帮帮我吗? My program is below.我的程序如下。
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)
I would add我会补充
except [errortype]:
pass
after the try
block.在try
块之后。 Replace [errortype] with the error that you hoped to reduce with the try block.将 [errortype] 替换为您希望通过 try 块减少的错误。 I don't see what error could be raised within that block, to you could likely just write我看不出该块内可能会引发什么错误,您可能只是写
turtle.xcor = value.real * 25 + 100
turtle.ycor = value.imag * 25 + 100
and remove the try block all together.并一起删除 try 块。
Besides the missing except
clause syntax error that @dguis points out (+1), I wonder what you think these lines are doing:除了@dguis 指出的缺少except
子句语法错误(+1)之外,我想知道您认为这些行在做什么:
turtle.xcor = value.real * 25 + 100
turtle.ycor = value.imag * 25 + 100
If .xcor
and .ycor
are your own properties that you've stashed on a turtle instance, then it's OK.如果.xcor
和.ycor
是您自己在海龟实例上隐藏的属性,那么没关系。 If you think this moves the turtle -- then not.如果你认为这会移动乌龟——那么不会。 If the goal is to move the turtle, try:如果目标是移动海龟,请尝试:
turtle.setx(value.real * 25 + 100)
turtle.sety(value.imag * 25 + 100)
Complete solution with additional tweaks:带有额外调整的完整解决方案:
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.