【发布时间】:2021-01-03 14:00:13
【问题描述】:
问题:给定一个整数列表,如果该数组在某处的某个 3 旁边包含一个 3,则返回 True。
经验: has_33([1, 3, 3]) → 真
我的代码:
def has_33(nums):
for items in nums:
if items == 3:
if nums[nums.index(items)+1] == 3:
return True
else:
continue
return False
使用此代码,我无法得到我想要的答案。我现在得到的答案是: has_33([1, 3, 1, 3]) → 假 has_33([3, 1, 3]) → 假 has_33([3, 1, 3, 3]) → False #连“3”旁边也是“3”。
【问题讨论】:
-
至于具体为什么这不起作用。请阅读
index的documentation 及其作用。应该很清楚为什么这不起作用
标签: python-3.x