我遇到这个CodingBat问题:
给定int数组长度2,如果它包含2或3,则返回True.
我尝试了两种不同的方法来解决这个问题.谁能解释我做错了什么?
#This one says index is out of range, why?
def has23(nums):
for i in nums:
if nums[i]==2 or nums[i]==3:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
#This one doesn't past the test if a user entered 4,3.
#It would yield False when it should be true. Why?
def has23(nums):
for i in nums:
if i==2 or i==3:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)