# Select segment IDs in a Segmentation with multiple segments

**URL:** <https://discourse.slicer.org/t/select-segment-ids-in-a-segmentation-with-multiple-segments/33667>\
**Category:** Support\
**Tags:** segmentation, segment\
**Created:** [January 8, 2024, 11:01am UTC](https://discourse.slicer.org/t/select-segment-ids-in-a-segmentation-with-multiple-segments/33667 "2024-01-08T11:01:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SANTIAGO\_PENDON\_MING](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/santiago_pendon_ming/32/66060_2.png) [@SANTIAGO\_PENDON\_MING](https://discourse.slicer.org/u/SANTIAGO_PENDON_MING)\
**Post date:** [January 8, 2024, 11:01am UTC](https://discourse.slicer.org/t/select-segment-ids-in-a-segmentation-with-multiple-segments/33667/1 "2024-01-08T11:01:12Z")

</div>

Hi to everyone. Today I’m trying to solve a very concerning problem. The situation is the next one:

```auto
suffix = '_manualSegmentation'
segmentationName = f"{patientID}{suffix}"
segmentName = f"{patientID}_coronaryTree{suffix}"
namesCorrect = False
  
while(not namesCorrect):
    try:
        segmentationNode = slicer.util.getNode(segmentationName)
        segmentId = segmentationNode.GetSegmentation().GetSegmentIdBySegmentName(segmentName)
        
        if (not segmentId == ''):
            namesCorrect = True
            
    except:
        input("Check that segmentation name and segment name are: " + 
              segmentationName + " and " + segmentName + ",respectively."
              + "And press key to continue...")
        namesCorrect = False

```

This code works properly to be sure that the user puts fine the segmentation and segment names. But only when I have one segment. If I have more than one, the code crashes and Slicer stops working… Am I doing something wrong ?

Thanks to everyone 🙂

---

<div class="post-metadata">

**Author:** ![Sunderlandkyl](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/sunderlandkyl/32/79987_2.png) [@Sunderlandkyl](https://discourse.slicer.org/u/Sunderlandkyl)\
**Post date:** [January 8, 2024, 4:04pm UTC](https://discourse.slicer.org/t/select-segment-ids-in-a-segmentation-with-multiple-segments/33667/2 "2024-01-08T16:04:57Z")

</div>

I think the code is just getting caught in an infinite while loop where namesCorrect is never True.

I’ve tested the code on a segmentation with multiple segments. If the segment name is found then it works fine, but if “segmentName” is not the name of a segment, there will be an infinite loop.
