title: python function skipping around
body: <p>My intent is to read a list from a file, pick a random number from that list and remove it, then resave the list. Sometimes the function works fine, sometimes not.</p>
  <pre><code>choices=[]
  
  with open(&quot;C:\\choices.txt&quot;, &quot;r&quot;) as f:
      for line in f: 
          choices.append(int(line.strip()))
          
  if len(choices)==0:
          choices=[0,1,2,3,4,5]
  
  num=rdm.randint(0,5)
  
  def chooseline(myList, myNum):
      print(&quot;before if num= &quot;+str(myNum))
      
      if not myNum in myList:
          myNum=rdm.randint(0,5)
          chooseline(myList,myNum)
          print(&quot;In NOT-IF num= &quot;+str(myNum))#&lt;-- Need to move before chooseline to print. ok
      else:
          myNum=myList.pop(myList.index(myNum))      
          print(&quot;in Else num= &quot;+str(myNum))
      
      print(&quot;end num= &quot;+str(myNum))
      return myNum
  
  
  newnum= chooseline(choices,num)
  
  with open(&quot;C:\\choices.txt&quot;, &quot;w&quot;) as f:
      for e in choices:
          f.write(str(e) +&quot;\n&quot;)
  </code></pre>
  <p>As long as the random number is in the list the function returns as expected, but if the first number is not on the list, it seems to loop back.</p>
  <pre><code>runfile('...')
  before if num= 0
  in Else num= 0
  end num= 0
  
  runfile('...')
  before if num= 2
  in Else num= 2
  end num= 2
  
  runfile('...')
  before if num= 2 #ok not in list 
  before if num= 0 #ok chooses new number, but not in list
  before if num= 4 # chose 4, in list
  in Else num= 4 # as expected
  end num= 4 #&lt;----- expect to stop here and return 4
  In NOT-IF num= 4
  end num= 4 #
  In NOT-IF num= 0
  end num= 0
  </code></pre>