0

I am not sure if it is iterating through the list or being called multiple times... in either case it shouldn't be doing either of those things. I am pretty new to python and programming in general. Here is the function:

"""
    Gets the mass that may have an isotope of +1 neutron for C,N or O (if      there is one)  
   @param mass     the mass
   @return  the mass +1
"""
def getMassPlus(mass):
    if (mass)+1 in mass_old2:
        return (mass)+1
    elif (mass)+1.1 in mass_old2:
        return (mass)+1.1
    elif (mass)+1.2 in mass_old2:
       return (mass)+1.2
    elif (mass)+.9 in mass_old2:
       return (mass)+.9

and here is the function call:

if ((14.00674*multiplyByN)+(15.994915*multiplyByO)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or isotope_masses[i]-.5: 
      if isotope_masses[i]+1 or isotope_masses[i]+1.1 or isotope_masses[i]+1.2 or isotope_masses[i]+.9 in mass_old2:
        nextMass = getMassPlus(isotope_masses[i])

What happens is that when I call the function, it somehow iterates through the list isotope_masses (which looks like this: isotope_masses = [1,2,3,4,...]) ten times and assigns nextMass ten values as well. It might also help you to know that the second block of code is part of a larger loop that iterates through the length of isotope_masses. I am not really sure why this issue happening, any ideas would be appreciated. Thanks in advance.

Loop:

for i in range(len(isotope_masses)):
    if not noNitrogen(molecules[i]):   #molecule has nitrogen and possibly hydrogen
    if hasDoubleDig('N'):
        multiplyByN = int(molecules[i][3:5])
        multiplyByH = int(molecules[i][8:])
    else:
        multiplyByN = int(molecules[i][3])
        multiplyByH = int(molecules[i][7:])
    if ((14.00674*multiplyByN)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or isotope_masses[i]-.5:
        if isotope_masses[i]+1 or isotope_masses[i]+1.1 or isotope_masses[i]+1.2 or isotope_masses[i]+.9 in mass_old2:
            nextMass = getMassPlus(isotope_masses[i])
            if float(intensities[mass_old2.index(nextMass)])/float(intensities[mass_old2.index(isotopes_masses[i])]) == multiplyByN * .00364:
                file_isotopes.append("Isotope: is N-15") 
            else:
                file_isotopes.append("Mix of isotopes")
    elif not noCarbon(molecules[i]):   #molecule has carbon and possibly hydrogen
    if hasDoubleDig('C'):
        multiplyByC = int(molecules[i][1:3])
        multiplyByH = int(molecules[i][8:])
    else:
        multiplyByC = int(molecules[i][1])
        multiplyByH = int(molecules[i][7:])
    if ((12.0107*multiplyByC)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or isotope_masses[i]-.5:
        if isotope_masses[i]+1 or isotope_masses[i]+1.1 or isotope_masses[i]+1.2 or isotope_masses[i]+.9 in mass_old2:

            print isotope_masses[i]

            nextMass = getMassPlus(isotope_masses[i])
            if float(intensities[mass_old2.index(nextMass)])/float(intensities[mass_old2.index(isotope_masses[i])]) == multiplyByC * .0107:
                file_isotopes.append("Isotope is: C-13")
            else:
                file_isotopes.append("Mix of isotopes")

There is more to the loop but its just repetitive so I only gave two cases in the loop for the sake of brevity.

Syd
  • 401
  • 2
  • 5
  • 13
  • Can you post the rest of the code? There's no loop of any kind that happens in your code right now and it's pretty difficult to answer a question about loops without knowing the structure of the loop. – Matt Habel Jul 15 '15 at 18:13
  • Ok, I added it to my post. Let me know if you need more info – Syd Jul 15 '15 at 18:17

2 Answers2

0

I think your if statements are always equating to True.

instead of:

if ((14.00674*multiplyByN)+(15.994915*multiplyByO)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or isotope_masses[i]-.5

you need the second part of the conditional to also be a full comparison:

if ((14.00674*multiplyByN)+(15.994915*multiplyByO)+(1.00794*multiplyByH)) == isotope_masses[i]+.5 or ((14.00674*multiplyByN)+(15.994915*multiplyByO)+(1.00794*multiplyByH)) == isotope_masses[i]-.5

same thing with the second conditional you have in there.

Martin Lear
  • 262
  • 1
  • 7
  • Ah, that helped. But then why does it give me 10 iterations instead of just 5? – Syd Jul 15 '15 at 18:25
  • My guess is because you are doing it again somewhere else in your code as well. Look through your if statements and make sure you are looking for direct comparisons in all of them. – Martin Lear Jul 15 '15 at 18:27
0
if isotope_masses[i]+1 or isotope_masses[i]+1.1 or isotope_masses[i]+1.2 or isotope_masses[i]+.9 in mass_old2:

equivalent to:

if a or b or c in x:

is being evaluated as

if (bool(a) or bool(b) or bool(c)) in x:

You need to change it to the following form:

if a in x or b in x or c in x:

Pretty sure this is a duplicate of How do I test one variable against multiple values?

Community
  • 1
  • 1
wwii
  • 23,232
  • 7
  • 37
  • 77