返回true寫功能lucky_sevens(數字),它接受的 整數的數組,並且如果任意三個連續元素之和爲7。幸運第七集,連續3數之和在陣列== 7
爲什麼不生成True輸出?最後3個值之和= 7
def lucky_sevens(numbers):
x, y = 0, 3
sum_of_numbers = sum(numbers[x:y])
while (sum_of_numbers != 7) and (y < len(numbers)):
x = x + 1
y = y + 1
if sum_of_numbers == 7:
return True
else:
return False
print(lucky_sevens([1,2,3,4,5,1,1]))
你從未改變'sum_of_numbers'。 – TigerhawkT3
While函數不會改變x和y的值,改變sum_of_numbers的值嗎? – Jason
爲什麼要改變和的值改變sum_of_numbers的值? – kindall