【发布时间】:2016-12-08 10:51:07
【问题描述】:
问题是:返回数组中数字的总和,空数组返回0。除了数字 13 很不吉利,所以它不算数,紧跟在 13 之后的数字也不算数。
我收到了error: list index out of range。我认为这是我的 for 循环的问题,但我不明白为什么这段代码不会运行。我该如何解决这个问题?
def sum13(nums):
unlucky = False
if sum(nums) == 0:
return 0
for num in nums:
x = 0
if nums[x] == 13:
unlucky = True
break
x += 1
if unlucky == True:
return sum(nums) - 13 - nums[x+1]
if unlucky == False:
return sum(nums)
【问题讨论】:
标签: python