“为什么?”;海龟.pd“;在我的Python代码中产生语法错误?
我试图制作一种复杂的参数图示器,但这并不重要。重要的是,我的程序应该使用Turtle图形绘制一个圆,当我放下笔时,““为什么?”;海龟.pd“;在我的Python代码中产生语法错误?,python,python-3.x,turtle-graphics,python-turtle,Python,Python 3.x,Turtle Graphics,Python Turtle,我试图制作一种复杂的参数图示器,但这并不重要。重要的是,我的程序应该使用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 cma
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
在之后,尝试块。将[errortype]替换为希望通过try块减少的错误。我看不出在那个块中会出现什么错误,因为你可能只需要写
turtle.xcor = value.real * 25 + 100
turtle.ycor = value.imag * 25 + 100
然后一起删除try块。除了@dguis指出(+1)的子句语法错误之外,我想知道您认为这些行在做什么:
turtle.xcor = value.real * 25 + 100
turtle.ycor = value.imag * 25 + 100
如果.xcor
和.ycor
是您自己的属性,并且您已经将它们保存在一个turtle实例中,那么就可以了。如果你认为这会移动乌龟,那么就不会了。如果目标是移动海龟,请尝试:
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)
尝试
需要一个,除了
。一般来说,如果您在某个意外的地方遇到一个语法错误
,请查看前一行/块,看看您是否忘记关闭某个东西,例如)
,或者在本例中,除了之外的。没有解决问题“没有解决问题”不是一个有用的回答。请阅读。您对代码做了哪些具体更改?为什么首先要尝试呢?为什么只在块中通过而不通过?为什么try
在那里?如果不了解上下文,不知道要防止什么,就不能推荐一个好的异常处理策略。是的,这就是为什么我还建议他们一起删除try-except块的原因。我理解传入except块是愚蠢的,但是@WilliamPowell可以将pass
更改为他们想要的任何其他内容。