python - Turtle 参数化代码可以在 Python 3 中运行,但不能在 Python2 中运行

标签 python python-3.x turtle-graphics

我有以下代码使用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 输出曲线(错误的):-

enter image description here

Python 3 中的turtle 曲线(右侧):-

enter image description here

任何人都有任何想法。我认为 math.sin 接受弧度,并且我根据转换和比例因子输入弧度。

最佳答案

整数除法在版本 2 中使用截断。在版本 3 中它会产生浮点结果。尝试更改

i/1000

i/1000.0

关于python - Turtle 参数化代码可以在 Python 3 中运行,但不能在 Python2 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32620081/

相关文章:

python - 如何让机器人每 5 分钟或由于其他事件发布/发送一条消息?

python - 将嵌套字典从 json 转换为以值作为列的数据框

c++ - 在 C++ 中捕获 Python 异常

python - 使 Python turtle 窗口大小与 Canvas 大小相同

python - 如何将图像导出到 GIMP 中的子目录?

python - int(x[,base]) 是如何工作的?

python-3.x - 尽管使用存储 key 进行连接,azure storage gen2 上的 set_access_control 仍会引发权限错误

python - 如何使用 Python 打开 AutoCAD 的一个实例中目录中的所有 .dwg 文件

python - Tkinter:如何限制用户使用多种功能

python - if/elif 语句和turtle 命令出错