I am very new to coding, two weeks in. So I do apologize if this is a very silly question.
I have been trying to complete the following codingbat 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.
Why does the below code not return the correct answer?
def array_front9(nums):
if len(nums)>4:
count = 4
else:
count = len(nums)
for i in range(0,count):
if nums[i]==9:
return True
else:
return False
If the return False is placed on a new line and not in the loop it works.
Can someone please explain this to me.