该程序的目标是让函数“Fib”接收两个值,并将它们放入一个斐波那契数列,在变量“sequence”中添加项。当它通过'check'函数并返回Limit Reached时,它会将偶数添加到列表'final'中,然后循环打印出'final'的总和。
问题是,无论 Fib 采用什么值,'final' 总是以没有值结束。我对编程很陌生,似乎无法弄清楚为什么要这样做......
def even(x):
v = list(str(x))[-1]
if v == '0' or v == '2' or v == '4' or v == '6' or v == '8':
return x
else:
return 0
def check(sequence):
for v in sequence:
if v >= 20:
return 'Limit Reached'
else:
return None
def Fib(x,y):
sequence = [x,y]
a = 0
b = 1
final = []
while len(sequence) < 100:
term = sequence[a] + sequence[b]
sequence.append(term)
if check(sequence) == 'Limit Reached':
for v in sequence:
final.apppend(even(v))
print sum(final)
break
a += 1
b += 1