gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:29:12 32 4
gpt4 key购买 nike

我有以下代码使用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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com