小编Jod*_*son的帖子

如果数组包含2或3,则返回True

我遇到这个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)

python python-2.7

3
推荐指数
1
解决办法
1664
查看次数

标签 统计

python ×1

python-2.7 ×1