新手入门,记录一下在新手村遇到的难题。
List 2 - sum13
Return the sum of the numbers in the array, returning 0 for an empty array. Except the number 13 is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count.
sum13([1, 2, 2, 1]) → 6
sum13([1, 1]) → 2
sum13([1, 2, 2, 1, 13]) → 6
def sum13(nums):
i = 0
result = 0
while i < len(nums): #用while函数逐个查找nums里面的数字,并且限定了范围
if nums[i] == 13:
i += 1 #如为true,i+1后会跳到第八行,变成i+2,跳过了13后面的数字
else:
result = result + nums[i] #把不等于13的数字相加
i += 1 检查下一个数字
return result
看了sum67的答案后试图改写sum13答案,未果。
List2 - sum67
Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 an