I'm very new to programming and I'd like your expertise on a problem I've come across on CodingBat. But I am not sure why my solution doesn't work. What am I missing here?
THE PROBLEM
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.
array_front9([1, 2, 9, 3, 4]) → True
array_front9([1, 2, 3, 4, 9]) → False
array_front9([1, 2, 3, 4, 5]) → False
MY SOLUTION
def array_front9(nums):
for i in range(0,len(nums)):
if len(nums)> 0 and nums[i] == 9:
return True
else:
return False
SOLUTION ONLINE
def array_front9(nums):
# First figure the end for the loop
end = len(nums)
if end > 4:
end = 4
for i in range(end): # loop over index [0, 1, 2, 3]
if nums[i] == 9:
return True
return False
Your assistance is appreciated. Thank you all.

