I have a function that I use on two different machine one a Mac running Python version 2.6 and the other is a Lenovo running version 3.2.The function writes data to a file and is called from with in a loop. When using Python 3.2 it works as expected and I get output such as below
25.0 25.0 25.0 0
25.0 25.0 75.0 0
25.0 25.0 125.0 0
25.0 25.0 175.0 0
25.0 25.0 225.0 0
25.0 75.0 25.0 0
25.0 75.0 75.0 0
25.0 75.0 125.0 0
25.0 75.0 175.0 0
25.0 75.0 225.0 0
When I run it on the machine running version 2.6 I get this
175.0 25.0 75.0 2
175.0 25.0 125.0 0
25.0 25.0 25.0 0 Should be first line
175.0 25.0 175.0 0
25.0 25.0 75.0 0 Should be second line
175.0 25.0 225.0 0
25.0 25.0 125.0 1
175.0 75.0 25.0 0
25.0 25.0 175.0 1
175.0 75.0 75.0 2
Here is the code
def filesave(Xc,Yc,Zc,S):
Xc = str(Xc)
Yc = str(Yc)
Zc = str(Zc)
Xs = str(S)
#Ms = str(Ma)
w = open("Myout.txt.","a+")
w.write(Xc)
w.write('\t')
w.write(Yc)
w.write('\t')
w.write(Zc)
w.write('\t')
w.write(Xs)
w.write('\n')
w.close()
return()
Is there some difference between the two versions that is causing the difference? Thanks!
EDIT
Rest of Code
def cell_centers():
read_file(F)
dx = dy = dz= float(input('Please enter a value for dr:')) #length of cell side
N = int(input('Please enter a value for N:')) #N^3 Number of cells to be created
Xc = zeros(N) #array creation
Yc = zeros(N)
Zc = zeros(N)
x1=0
y1=0
z1=0
county = 0
countz = 0
for i in range(N): #for loops to define cell centers
Xc[i] = dx/2 +x1
xmin = Xc[i]-dx/2
xmax = Xc[i]+dx/2
x1+=dx #increments x1 positions by dx
for j in range(N):
Yc[j] = dy/2 +y1
ymin = Yc[j]-dy/2
ymax = Yc[j]+dy/2
county+=1
if county==N: #if else statement resets y1 to zero
y1=0
county=0
else:
y1+=dy
for k in range(N):
Zc[k] = dz/2 +z1
countz+=1
zmin = Zc[k]-dz/2
zmax = Zc[k]+dz/2
if countz==N:
z1=0
countz=0
else:
z1+=dz
counter(Xc[i],Yc[j],Zc[k],N,xmin,xmax,ymin,ymax,zmin,zmax,*read_file(F))
return()
def counter(Xc,Yc,Zc,N,xmin,xmax,ymin,ymax,zmin,zmax,Xa,Ya,Za):
Cellcount = zeros(1)
S = (((xmin <= Xa) & (Xa <= xmax))& #count what is in specific range
((ymin <= Ya) & (Ya <= ymax))&
((zmin <= Za) & (Za <= zmax))).sum()
for l in range(1):
Cellcount[l]= S
filesave(Xc,Yc,Zc,S)
return()
I am going to go out on the limb and say the difference you are observing is due to the changed division between version 2.x and 3.x. (it looks like there’s a lot of dividing going on, and I can’t tell what type the numbers are, integer or float)
In 2.x you would get integer truncation when doing division with integers. This doesn’t happen in v 3.x
Python 2
Python 3:
Your code does a lot of division.
If you still want to old integer division behavior, you can use
//with Python 3:Python 3:
Changing the Division Operator explains this in detail.
What’s New In Python 3.0 goes over the big changes from v 2 to 3
If you want the new division behavior in Python 2.2+, you can use the
from __future__ import divisiondirective (Thanks @Jeff for reminding me).Python 2:
UPDATE:
Finally, please consider the potential problem of division as a cause (so perhaps the lines aren’t out of order, but the results are different due to the division making it only appear that way). Is that possible? Also notice that the 4th column (the 3.x output) has all zeros .. that’s not present in the 2.x output and further points toward possible problems with the computation of results — so in fact the results are different and not out of order.