0

I am trying to define a function which calculates the distance between two particles and prints out a table containing the distances. However, when I use the function, nothing is printed. What am I doing wrong?

au = 1.49598e11                        #astronomical unit in meters
rx = au * np.asarray([.5,.8,.2])       #x-comp separation vector
ry = au * np.asarray([2.6,9.1,3.7])    #y-comp separation vector
rz = au * np.asarray([.05,.1,.25])     #z-comp separation vector
def svec(x,y,z):
    '''computes the magnitude and vector components of the distance between two particles.'''

    #for loop to compute vector components of separation between two particles
    rvec = []
    for i in range(3):
        if i < 2:
            vx = x[i]-x[i+1]
            vy = y[i]-y[i+1]
            vz = z[i]-z[i+1]
            rvec.append([vx,vy,vz])
         if i == 2:
             vx = x[0]-x[-1]
             vy = y[0]-y[-1]
             vz = z[0]-z[-1]
             rvec.append([vx,vy,vz])
    return rvec
    comp1 = ['x-comp[m]','y-comp[m]','z-comp[m]']
    r0 = rvec[0].insert(0,'particle 0->1')
    r1 = rvec[1].insert(0,'particle 1->2')
    r2 = rvec[2].insert(0,'particle 0->2')
    print(tabulate(rvec,headers=comp1))
2
  • Now everything beyond the return line will not be executed. Did yuo mean to indent that too? Commented Jun 16, 2014 at 20:44
  • Moving everything that was beyond the return line to coming before the return line fixed the issue. Thank you, Martijn!!! Commented Jun 16, 2014 at 20:50

2 Answers 2

1

Python is whitespace sensitive, but docstrings may prevent you from getting a proper error due to your indentation.

Looks like your code should be indented like this:

au = 1.49598e11                        #astronomical unit in meters
rx = au * np.asarray([.5,.8,.2])       #x-comp separation vector
ry = au * np.asarray([2.6,9.1,3.7])    #y-comp separation vector
rz = au * np.asarray([.05,.1,.25])     #z-comp separation vector
def svec(x,y,z):
    '''computes the magnitude and vector components of the distance between two particles.'''

    #for loop to compute vector components of separation between two particles
    rvec = []
    for i in range(3):
        if i < 2:
            vx = x[i]-x[i+1]
            vy = y[i]-y[i+1]
            vz = z[i]-z[i+1]
            rvec.append([vx,vy,vz])
        if i == 2:
            vx = x[0]-x[-1]
            vy = y[0]-y[-1]
            vz = z[0]-z[-1]
            rvec.append([vx,vy,vz])
    return rvec

comp1 = ['x-comp[m]','y-comp[m]','z-comp[m]']
r0 = rvec[0].insert(0,'particle 0->1')
r1 = rvec[1].insert(0,'particle 1->2')
r2 = rvec[2].insert(0,'particle 0->2')
print(tabulate(rvec,headers=comp1))
Sign up to request clarification or add additional context in comments.

Comments

0

The indentation in your code sample seems off: you define a function but the function body is not indented. Can you make sure that the indentation in the pasted code matches what you're running?

This is important because if your print statement comes after the return statement in the function, it would explain why nothing is printed. (Unlike in some other languages, Python will not complain if you have unreachable code after a return statement.) But without seeing the actual indentation in your program, it's impossible for us to say.

EDIT: Now that the indentation has been fixed, I can confirm that the return statement will indeed prevent everything after it from being executed. Perhaps you meant to dedent all of the lines after the return statement?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.