[英]I'm not able to step up terms in fibonacci sequence python
我想打印 fibonacci seq 的每個第四項,直到 n 項,但是我很困惑為什么這段代碼不起作用,我首先有一個函數來計算 n 項,然后是另一個函數來迭代和每第四項步進。
# Program to display the Fibonacci sequence up to n-th term
def fibo_sequence(n):
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if n <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
def fibo_skipper(n):
#fibo_sequence(n)
for term in fibo_sequence(n):
for i in range(0, n, 4):
print(i)
n = 8 #Enter the nth term here
fibo_skipper(n) # Print the print every fourth item of the Fibonacci sequence upto n
問題是您在最低級別使用print
,因此無法從更高級別過濾任何內容。 如果您希望能夠過濾,則低fibo_sequence(n)
函數不應打印任何內容,而應返回一個序列。 一個簡單的方法是使用列表:
# Program to display the Fibonacci sequence up to n-th term
def fibo_sequence(n):
seq = []
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if n <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif n == 1:
print("Fibonacci sequence upto",n,":")
seq.append(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < n:
seq.append(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
return seq
但它仍然不會遵循良好的封裝實踐,因為相同的函數顯示消息並構建序列。 您應該只構建序列(可選的空列表)並讓調用者決定在返回列表的長度為 0 或 1 時顯示什么。
如果你想更進一步,你應該考慮構建一個生成器,一次yield
一個值,因此避免存儲它們。
注意:我從假設您的fibo_sequence
實際上返回一個sequence
開始寫了一個答案......我的錯。
顯然,很自然的情況是您有某種斐波那契計算(即返回一個序列,使用生成器)返回一個可處理的輸出,正如許多解決方案所建議的那樣。
無論如何,你仍然可以做你喜歡做的事情,即使有打印到屏幕的功能,臨時重定向sys.stdout
from io import StringIO
import sys
def fibo_skipper(n, step = 4):
old_stdout = sys.stdout
sys.stdout = StringIO()
fibo_sequence(n)
lines = sys.stdout.getvalue().split("\n")[1:] # [1:] is to skip first line
sys.stdout = old_stdout
# slice operator will split your lines every n elements
print(lines[::step])
fibo_skipper(100)
重要的是要強調,我不是在建議這種方法(這超出了您原來的問題),但是,在您無法完全控制被調用方法(在您的情況下為fibo_sequence
)的情況下,這仍然可以讓您到達需要。
作為旁注,我認為您可以更緊湊的else
語句:
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
# update values
n1, n2 = n2, n1 + n2
count += 1
在您當前的代碼結構中,您應該在打印之前檢查第n-th
元素是否可以被 4 整除,並且您可以使用模數運算符%
檢查
...
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < n:
if count % 4 == 0:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
通過這種方式,您仍然可以計算系列並僅打印每個第 4 個元素。
如果您希望跳過大小作為參數,您可以將其傳遞給您的函數:
def fibo_sequence(n, skip):
...
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < n:
if count % skip == 0:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
請注意,此代碼結構對更改的魯棒性較差。
您將在 main 中調用 skipper 函數。 skipper 函數調用序列函數,該函數返回斐波那契數列。 for 循環以 4 的間隔打印這些數字。
# Program to display the Fibonacci sequence up to n-th term
def fibo_sequence(n):
l = []
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if n <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif n == 1:
print("Fibonacci sequence upto",n,":")
l.append(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < n:
l.append(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
return l
def fibo_skipper(n):
l = fibo_sequence(n)
for i in range(0, n, 4):
print(l[i])
n = int(input())
fibo_skipper(n)
我的第一個答案是垃圾,給你:
def fibo_sequence(n):
n1, n2 = 0, 1
count = 0
seq = []
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print("Fibonacci sequence upto",n,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < n:
print(n1)
seq.append(n1)
nth = n1 + n2
# update values
n1, n2 = n2, nth
count += 1
if len(seq) != 0:
return seq
def fibo_skipper(n):
print("Every 4th Number in Fibo up to n: ")
seq = fibo_sequence(n)
for el in seq:
print(seq[el*4])
n = 10
fibo_skipper(n)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.