How do I preform a check only once?
<p>So I have this question that I have to do for homework.</p>

<p>Next, write ind( e, L ). Here is its description:</p>

<p>Write ind(e, L), which takes in a sequence L and an element e. L might be a string or, more generally, a list. Your function ind should return the index at which e is first found in L. Counting begins at 0, as is usual with lists. If e is NOT an element of L, then ind(e, L) should return the integer equal to len(L). Here are a few examples:</p>

<pre><code>ind(42, [ 55, 77, 42, 12, 42, 100 ]) returns 2
ind(42, range(0,100)) returns 42
ind('hi', [ 'hello', 42, True ]) returns 3
ind('hi', [ 'well', 'hi', 'there' ]) returns 1 
ind('i', 'team') returns 4 
ind(' ', 'outer exploration') returns 5
</code></pre>

<p>In this last example, the first input to ind is a string of a single space character, not the empty string.</p>

<p>Hint: Just as you can check whether an element is in a sequence with</p>

<p>if e in L:
you can also check whether an element is not in a sequence with</p>

<p>if e not in L:
This latter syntax is useful for the ind function! As with dot, ind is probably most similar to mylen from the class examples.</p>

<p>And here it once of the many codes I have written for this problem.</p>

<pre><code>def ind( e, L):
  num = 0
  if e not in L and num == 0:
    return len(L)
  else:
    if e == L[0]:
      num += 1 + ind( e, L[1:] )
      return num
    else:
      return ind( e, L[1:] )
</code></pre>

<p>So the problem is that everytime e is no longer in the list.  It takes the length of the remainder of the list and adds that to num.  How do I fix this???</p>

<blockquote>
  <p>And here it(!) once(!) of the many codes I have written for this problem.</p>
</blockquote>

<p>You should pay more attention to what you're writing and probably also to what you are coding.</p>

<p>Unless you are explicitly asked to write a recursive function for this task, I'd advise against it.</p>

<p>Try to break down the task to smaller subproblems. Some of them are already solved:</p>

<ul>
<li>If the element is not contained in the list, return the length of the list (done)</li>
<li>Loop over the list, keeping track of the current index

<ul>
<li>if the current list element is the one you are looking for, return the current index</li>
<li>else, look at the next element</li>
</ul></li>
<li>since you can be sure that the element is in the list once you enter the loop (why?), you are done.</li>
</ul>

<hr>

<p>If you have to do it recursively, the "hints" are rather misleading. The <code>not in</code> syntax is not useful. Did you look at the <code>mylen</code> function?
I suppose it looks like</p>

<pre><code>def mylen(s):
  if not s:
    return 0
  return 1 + mylen(s[1:])
</code></pre>

<p>Now, to find the (first) index of a given element:</p>

<pre><code> def ind(e, l):
   if not l:        # list is exhausted, element was not found
     return 0
   elif l[0] == e:  # element found
     return 0
   else             # element not found yet
     return 1 + ind(e, l[1:])
</code></pre>

python python-2.7