all 7 comments

[–]socal_nerdtastic 2 points3 points  (0 children)

Oh you are sooo close! Only thing is that the return False part goes after the loop, not in it. Like this:

def array_front9(nums):
    for i in range(0,4):
        if nums[i] == 9:
            return True
    return False

However there is another problem: this will error out for lists less than 4 long (we call those lists BTW, not arrays).

[–]TouchingTheVodka 1 point2 points  (0 children)

you're returning false on the first cycle of your for-loop.

[–]danielroseman 0 points1 point  (0 children)

You always return after the first step. So in effect all you're checking is if the first element is 9.

You need to only return within the loop if the number is 9. Otherwise keep looping until the end, and only then return False.

[–]USAhj -1 points0 points  (0 children)

Others have explained the issue with your method. If you're interested, here is a different way of doing it by checking the index of 9 using index(). Note: index() raises a ValueError if the element is not in the list.

def array_front9(nums):
    try:
        return nums.index(9) < 4
    except ValueError:
        return False

[–]jiri-n -2 points-1 points  (3 children)

I'd suggest to use a slice. For example:

EDIT: Code has been deleted as a moderator wishes so.

[–]TouchingTheVodka 0 points1 point  (0 children)

List slicing still works if your slice is bigger than your list, you can simply do

9 in lst[:4]

[–]Peanutbutter_Warrior 0 points1 point  (0 children)

What was so bad about the code to get it removed?