2015-09-16 38 views
1

我有以下代碼在python中使用海龜模塊繪製參數曲線。我不明白爲什麼這個工程在Python 3,而不是在Python 2海龜參數代碼在Python 3中工作,但不在Python2中

兩個代碼變種

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(錯誤的): -

enter image description here

而用於Python 3(右側的一個)在龜曲線: -

enter image description here

任何人都有任何想法。我認爲math.sin接受弧度,我輸入的弧度基於轉換,然後是比例因子。

+0

下方plot()你有一個可能錯誤間隔的for循環。 –

+0

謝謝,這是稍微不正確的格式,而我發佈代碼在stackoverflow –

回答

2

整數除法使用在第2版截斷它產生的浮點結果版本3.嘗試改用

i/1000 

i/1000.0 
+0

嘿,謝謝你做的伎倆。不能相信這是件微不足道的事情。我應該調試並檢查輸入值。 –

0

添加在begining PEP-0238

from __future__ import division 
相關問題