下面这段代码中,kind_num用于统计那个整数列表中有几种不同数值的整数。
class Solution(object):
def distributeCandies(self, candies):
"""
:type candies: List[int]
:rtype: int
"""
loc = len(candies)
mol = loc % 2
if not (2 <= loc <= 10000) or mol != 0:
return 'wrong length of array'
for num in candies:
if not (-10000 <= num <= 10000):
return 'wrong element in array'
kind_num = 0
sis_num = loc / 2
for candy in candies:
kind_num += 1
while True:
try:
candies.remove(candy)
print candies
except ValueError:
break
if kind_num > sis_num:
return sis_num
elif kind_num < sis_num:
return kind_num
else:
return sis_num
s = Solution()
print s.distributeCandies([1,1,2,2,3,3])
但是第二个for循环,没有取完candies里面的值就提前退出了,这是为什么???