Python 3.2 vs Python 2.7 code problems -


i have code graph mandlebrot set using pygame. here code.

import pygame, sys, math decimal import * window=pygame.display.set_mode((1000, 1000)) window.fill((255, 255, 255)) pygame.display.update() winrect=window.get_rect() hq=3 getcontext().prec=20 colors=((255, 0, 0), (255, 128, 0), (255, 255, 0), (128, 255, 0), (0, 255, 0), (0, 255, 128), (0, 255, 255), (0, 128, 255), (0, 0, 255), (128, 0, 255), (255, 0, 255), (255, 0, 128)) def graph(scale):#left, right, bottom, top     window.fill((0, 0, 0))     minimum=-1     y=((scale[3]-scale[2]))/(winrect.height)+scale[2]     in range(winrect.width):         x=((scale[1]-scale[0])*(a))/(winrect.width)+scale[0]         d, e=x**2-y**2+x, 2*x*y+y         in range(int(1/(50*(scale[1]-scale[0]))+25)):             d, e=d**2-e**2+x, 2*d*e+y             if math.sqrt(d**2+e**2)>2:                 if i<minimum or minimum==-1:                     minimum=i                 break     y=((scale[3]-scale[2])*winrect.height)/(winrect.height)+scale[2]     in range(winrect.width):         x=((scale[1]-scale[0])*a)/winrect.width+scale[0]         d, e=x**2-y**2+x, 2*x*y+y         in range(int(1/(50*(scale[1]-scale[0]))+25)):             d, e=d**2-e**2+x, 2*d*e+y             if math.sqrt(d**2+e**2)>2:                 if i<minimum or minimum==-1:                     minimum=i                 break     x=((scale[1]-scale[0])*1)/winrect.width+scale[0]     b in range(winrect.height):         y=((scale[3]-scale[2])*b)/winrect.height+scale[2]         d, e=x**2-y**2+x, 2*x*y+y         in range(int(1/(50*(scale[1]-scale[0]))+25)):             d, e=d**2-e**2+x, 2*d*e+y             if math.sqrt(d**2+e**2)>2:                 if i<minimum or minimum==-1:                     minimum=i                 break     x=((scale[1]-scale[0])*winrect.width)/winrect.width+scale[0]     b in range(winrect.height):         y=((scale[3]-scale[2])*b)/winrect.height+scale[2]         d, e=x**2-y**2+x, 2*x*y+y         in range(int(1/(50*(scale[1]-scale[0]))+25)):             d, e=d**2-e**2+x, 2*d*e+y             if math.sqrt(d**2+e**2)>2:                 if i<minimum or minimum==-1:                     minimum=i                 break     in range(winrect.width):         b in range(winrect.height):             x=((scale[1]-scale[0])*a)/winrect.width+scale[0]             y=((scale[3]-scale[2])*b)/winrect.height+scale[2]             d, e=x**2-y**2+x, 2*x*y+y             in range(minimum):                 d, e=d**2-e**2+x, 2*d*e+y             in range(20*hq):                 d, e=d**2-e**2+x, 2*d*e+y                 if math.sqrt(d**2+e**2)>2:                     window.set_at((a, b), colors[i-(int(i/len(colors))*len(colors))])                     break             event in pygame.event.get():                 if event.type==pygame.quit:                     pygame.quit()                     sys.exit()                 if event.type==pygame.keydown:                     if event.key==pygame.k_escape:                         pygame.quit()                         sys.exit()         pygame.display.update()     pygame.display.update() graph([-3, 2, -2.5, 2.5, 0])# scale=[-3, 2, -2.5, 2.5, 0] scalea=scale[:] while true:     event in pygame.event.get():         if event.type==pygame.quit:             pygame.quit()             sys.exit()         if event.type==pygame.keydown:             if event.key==pygame.k_escape:                 pygame.quit()                 sys.exit()             if event.key==pygame.k_r:                 graph([-3, 2, -2.5, 2.5, 0])                 scale=[-3, 2, -2.5, 2.5, 0]                 scalea=scale[:]             if event.key==pygame.k_p:                 hq+=1                 graph(scale)             if event.key==pygame.k_o:                 if not hq==1:                     hq-=1                     graph(scale)             if event.key==pygame.k_space:                 print(scale)                 print(scale[1]-scale[0])         if event.type==pygame.mousebuttondown:             if not scalea[4]:                 scalea[0]=(((scale[1]-scale[0])*event.pos[0])/winrect.width)+scale[0]                 scalea[2]=(((scale[3]-scale[2])*event.pos[1])/winrect.height)+scale[2]                 scalea[4]=1             else:                 scalea[1]=(((scale[1]-scale[0])*event.pos[0])/winrect.width)+scale[0]                 scalea[3]=(((scale[3]-scale[2])*event.pos[1])/winrect.height)+scale[2]                 scalea[4]=0                 if scalea[1]<scalea[0]:                     scalea=[scalea[1], scalea[0], scalea[2], scalea[3], 0]                 if scalea[3]<scalea[2]:                     scalea=[scalea[0], scalea[1], scalea[3], scalea[2], 0]                 scale=scalea[:]                 if scale[1]-scale[0]<scale[3]-scale[2]:                     scale[1]+=((scalea[3]-scalea[2])-(scalea[1]-scalea[0]))/2                     scale[0]-=((scalea[3]-scalea[2])-(scalea[1]-scalea[0]))/2                 else:                     scale[2]-=((scalea[1]-scalea[0])-(scalea[3]-scalea[2]))/2                     scale[3]+=((scalea[1]-scalea[0])-(scalea[3]-scalea[2]))/2                 graph(scale) 

when run in python 3.2, works fine. image looks this:

[img]http://i47.tinypic.com/2ps0d8n.jpg[/img]

however, when run in python 2.7, horrible looking image looks this:

[img]http://i47.tinypic.com/2a0nskk.jpg[/img]

is there way can fix this?

yes, add @ top of file:

from __future__ import division 

python 2 uses integer division (floor division) when using integer inputs default; python 3 switched floating point division when using integer inputs.

see pep 238, documents change:

the current division (/) operator has ambiguous meaning numerical arguments: returns floor of mathematical result of division if arguments ints or longs, returns reasonable approximation of division result if arguments floats or complex. makes expressions expecting float or complex results error-prone when integers not expected possible inputs.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -