我有以下代码使用turtle模块在python中绘制参数曲线。我不明白为什么这在 python 3 中有效,而在 python 2 中无效。
两种变体的代码
import turtle
import math
def line(x1,y1,x2,y2):
turtle.up()
turtle.goto(x1,y1)
turtle.down()
turtle.goto(x2,y2)
def plot():
turtle.up()
turtle.goto(0,150)
turtle.down()
for i in range(0, int(2*math.pi*1000),10):
turtle.goto(150*math.sin(2*(i/1000)),150*math.cos(5*(i/1000)))
def axes():
line(-200,0,200,0)
line(0,-200,0,200)
def main():
turtle.setup()
turtle.color("blue")
axes()
turtle.color("red")
plot()
turtle.done()
main()
Python 2 的 turtle 输出曲线(错误的):-
Python 3 中的turtle 曲线(右侧):-
任何人都有任何想法。我认为 math.sin 接受弧度,并且我根据转换和比例因子输入弧度。
最佳答案
整数除法在版本 2 中使用截断。在版本 3 中它会产生浮点结果。尝试更改
i/1000
至
i/1000.0
关于python - Turtle 参数化代码可以在 Python 3 中运行,但不能在 Python2 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32620081/