如果数字9在输入数组的前4位数字中,则应返回True。阵列长度可能小于4。
为什么此循环不起作用?在某些测试案例中,它仅返回True和False。
def array_front9(nums):
for num in nums:
first4 = nums[0:4]
if num in first4 and num == 9:
return True
else:
return False
如果数字9在输入数组的前4位数字中,则应返回True。阵列长度可能小于4。
为什么此循环不起作用?在某些测试案例中,它仅返回True和False。
def array_front9(nums):
for num in nums:
first4 = nums[0:4]
if num in first4 and num == 9:
return True
else:
return False
You don't need a
for
loop since thein
operator would iterate through the characters of a string for you. You should also compare a string to a string, rather than a number, which would never be equal to a string: