Error:list index out of range
<p>I am a beginner in Python and programming in general and I'm trying to solve a problem on codingbat.com (the problem is called "array_front9" under the section "Warmup-2"). </p>

<p>The problem is: "Given an array of ints, return True if one of the first 4 elements in the array is a 9. The array length may be less than 4."</p>

<p>Here is my code which works if I create a list and then run it locally (on codingbat.com it is necessary to create a function but I do not create a function to test my code locally):</p>

<pre><code>arr = [14,9,28,55,66,33,789,4548]

    for i in range (4):

        if arr[i] == 9:

            print('True')

    print('False')*
</code></pre>

<p>Here is the code I'm trying to run on codingbat.com but I receive "Error:list index out of range" error:</p>

<pre><code>def array_front9(arr):

    for i in range(4):

        if arr[i] == 9:

            return True

     return False
</code></pre>

<p>Here is the solution according to codingbat.com:</p>

<pre><code>def array_front9(nums):
  # First figure the end for the loop
      end = len(nums)
          if end &gt; 4:
              end = 4

for i in range(end):  # loop over index [0, 1, 2, 3]
    if nums[i] == 9:
      return True
    return False
</code></pre>

<p>Here is the current URL for this problem: <a href="http://codingbat.com/prob/p110166" rel="nofollow">http://codingbat.com/prob/p110166</a></p>

<p>Can anyone point me to what I'm doing wrong?</p>

<p>The problem is <em>"check the first four numbers of a list, to see if any of them is a 9"</em>.</p>

<p>Now, if the list is less than 4, then automatically the check will fail:</p>

<pre><code>if len(arr) &lt; 4:
  return false
</code></pre>

<p>Next, you need to check if the first four items contain nine. There are many ways to do this, one of the easiest is:</p>

<pre><code>for i in arr[:4]:
  if i == '9':
     return true
</code></pre>

<p>Combine the two:</p>

<pre><code>def check_for_nine(arr):

  if len(arr) &lt; 4:
     return false

  for i in arr[:4]:
    if i == '9':
       return true
</code></pre>

<p>The issue with your initial approach is you are forcing the length to be 4, and assuming that at minimum the array will have 4 elements. If the array has 3 elements, then your code will not work.</p>

<p>To avoid this, don't assume a specific length. If you use slicing, then you are guaranteed that you will get <em>at most 4</em> items.</p>

<p>Here is an example of this:</p>

<pre><code>&gt;&gt;&gt; i = [1,2]
&gt;&gt;&gt; i[:4]
[1, 2]
</code></pre>

<p>Even though <code>i</code> only has two items, the slice to 4 works and doesn't raise an error. Once <code>i</code> has more than 4 items, it will limit to the first four:</p>

<pre><code>&gt;&gt;&gt; i = [1,2,3,4,5,6,7,8]
&gt;&gt;&gt; i[:4]
[1, 2, 3, 4]
</code></pre>

<p>However, our problem statement specifically said that check if the first four items contain 9. If we just use the slice trick, like this:</p>

<pre><code>for i in arr[:4]:
    if i == '9':
       return true
</code></pre>

<p>It will return true even if we pass it an array like <code>['1','9']</code> since the slice will always work.</p>

<p>To catch this condition - we first check if the length of the list is less than 4, because if it is - it automatically fails the condition.</p>

python arrays list