简体   繁体   中英

Loop only prints last value

I have some working python code I am using in Abaqus, but I don't manage to make a loop out of it. Can someone help me with this rather simple problem? I would like to print x, y, z coordinates (called partxcord etc) for different SetsScrews. It only prints the last SetsScrews. What am I missing? Thank you in advance.

SetsScrews = ["Screw['T6_R']", "Screw['T7_R']", "Screw['T8_R']"]

for i in range(len(SetsScrews)):
   # Select nodeset
   PartLevel = mdb.models[modelname].rootAssembly.sets[SetsScrews[i]]
   print('ParTLEVEL=', PartLevel)

   # Calculate the amount of nodes in nodeset
   numNodes = len(PartLevel.nodes)

  # Create lists to write data to
  partlabel=[]
  partxcord=[]
  partycord=[]
  partzcord=[]

  for curNode in PartLevel.nodes:
        partlabel.append(curNode.label)
        partxcord.append(curNode.coordinates[0])
        partycord.append(curNode.coordinates[1])
        partzcord.append(curNode.coordinates[2])
        print('numNodes=', numNodes)
partlabel=[]
partxcord=[]
partycord=[]
partzcord=[]

these should be intialized outside the loop, maybe make a dictionary with screwnames and then add these lists there

or your indentation could be an issue too

If you want to just print the coordinates, then you just need print after inner loop ends. Also, you can store coordinates for all parts. Please refer below code:

SetsScrews = ["Screw['T6_R']", "Screw['T7_R']", "Screw['T8_R']"]
# create the list to save all coordinates
aLabels,axcoords,aycoords,azcoords = [],[],[],[]
for i in range(len(SetsScrews)):
    # Select nodeset
    PartLevel = mdb.models[modelname].rootAssembly.sets[SetsScrews[i]]
    print('ParTLEVEL=', PartLevel)
    
    # Calculate the amount of nodes in nodeset
    numNodes = len(PartLevel.nodes)

    # Create lists to write data to
    partlabel,partxcord,partycord,partzcord=[],[],[],[]
    print('Screw= ', SetsScrews[i])
    for curNode in PartLevel.nodes:
        partlabel.append(curNode.label)
        partxcord.append(curNode.coordinates[0])
        partycord.append(curNode.coordinates[1])
        partzcord.append(curNode.coordinates[2])
        # you can print it here...
        print(curNode.label,curNode.coordinates[0],curNode.coordinates[1],curNode.coordinates[2])
    
    # or you can print all data here and save it
    print('partlabel= ', partlabel)
    print('partxcord= ', partxcord)
    print('partycord= ', partycord)
    print('partzcord= ', partzcord)
    aLabels.append(partlabel)
    axcoords.append(partxcord)
    aycoords.append(partycord)
    azcoords.append(partzcord)

Now at the end of the both the for loops, you have all the coordinates in aLabels, axcoords, aycoords, azcoords these lists.

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