def fibonacci(n):
terms = [0,1]
i = 2
for i in terms[2:n+1]:
terms.append(terms[i-1] + terms[i-2])
return terms[n]
user_input= input ('Write the number order by which you want to know its corresponding value in the fibonacci sequence')
fibonacci_user_input = fibonacci(user_input)
print fibonacci_user_input
Pyscripter Python 2.7.9中引用的關於此程序的語義錯誤是,它返回值None
。 我剛剛開始學習Python,並且現在已經發現這個程序有什麼問題了。 我已經發現如何使用while循環和遞歸來編寫斐波那契數列程序,而我只是很難使用這個過程。我的斐波那契序列程序使用「for循環」有什麼問題
你在for循環中返回......這意味着無論N有多大都會在第一次迭代時返回... –
解釋這個實現相對於其他實現似乎有什麼問題。 – J0e3gan
提示:函數啓動時,「terms [2:n + 1]」的值是多少?你爲什麼將'terms'的當前內容用作'i'的值? – Barmar