user2185573
user2185573

Reputation:

Turtle parametric code working in Python 3 but not in Python2

I have the following code to plot a parametric curve in python using the turtle module. I dont understand why this works in python 3 and not in python 2.

The code for both variants

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()

Output curve in turtle for Python 2 (the wrong one):-

enter image description here

And the curve in turtle for Python 3 (the right one):-

enter image description here

Anyone has any idea. I think the math.sin accepts radians and i input radians based on by conversion followed by a scaling factor.

Upvotes: 1

Views: 257

Answers (2)

WBAR
WBAR

Reputation: 4984

Add at begining PEP-0238:

from __future__ import division

Upvotes: 0

Robert E
Robert E

Reputation: 401

Integer division used truncation in version 2. It yields floating point result in version 3. Try changing

i/1000

to

i/1000.0

Upvotes: 1

Related Questions