简体   繁体   中英

If else if else conditional loop in python

I've set of molecules given in molecules_group1 and i want to move the molecules to other new empty molecules_group2 via my function. It adds only one molecules to the empty molecule_group2. whenever i upload any moleculegroup containing one or more molecules it always prints "sorted first five molecules present in newgroup" without adding the molecule. furthermore if the molecule is not added due to tolerance criteria then how i can make the function run again until it is successful to add the molecule.

def initial_configurations(newemptygroup,backgroundmols,tolerance,boxspace):
    p = PointRef(backgroundmols.molecule().evaluate().centerOfGeometry())
    c = CloseMols(p,newemptygroup,5)
    g_random = c.closeMolecules()
    if len(g_random) == 0:
        newemptygroup.add(backgroundmols)
        print("first molecules to be added")
    else:
        g = sorted(g_random.items(), key=operator.itemgetter(1)) # sort the dict by value which give tuple
        print("sorted first five  molecules present in newgroup =%s" %g)
        t = [x[0] for x in g]
        selected = newemptygroup[t[0]].molecule().evaluate().centerOfGeometry()
        for i in range(1,len(t)):
            closemols = newemptygroup[t[i]].molecule().evaluate().centerOfGeometry()
            if (selected-closemols).length() > tolerance:
                newemptygroup.add(backgroundmols)
            else:
                print("not added")           
    return newemptygroup

You could insert a break statement right after newemptygroup.add(backgroundmols)

...
if (selected-closemols).length() > tolerance:
  newemptygroup.add(backgroundmols)
  break
else:
  print("not added")   
...

This assumes that you will find the correct backgroundmols at sometime within your for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM