我的代码有问题。问题是:
返回数组中数字的总和,空数组返回0。除此之外,数字13是非常不吉利的,所以它不算数,紧跟在13之后的数字也不算数。 发件人:https://codingbat.com/prob/p167025
def sum13(nums):
if not nums:
return 0
for x in nums:
if x == 13:
break
return sum(x)
if 13 not in nums:
return sums(nums)
问题是我不能合并和(x),每次尝试时它都会给出一个错误。 有人能暗示一下原因吗
错位
return
是这里的许多问题之一,但可能是修复逻辑的良好开端。很难找出一个问题,一旦解决,将产生预期的结果。相反,存在一个普遍的问题,即实现与任务逻辑无关通常,您会将问题分解为更简单的砖块,有点像这样:
一旦您有了基线解决方案,就要使它变得更好,例如使用迭代器:
相关问题 更多 >
编程相关推荐