我无法提高斐波那契数列的条件 python
I'm not able to step up terms in fibonacci sequence python
我想打印斐波那契数列的每第四项直到 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
我的第一个答案是垃圾,给你:
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)
注意:我写了一个答案,假设你的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中调用船长函数。船长函数调用序列函数,其中 returns 斐波那契数列。 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)
问题是您在最低级别使用 print
,因此无法从更高级别过滤任何内容。如果你想过滤,low fibo_sequence(n)
函数不应该打印任何东西,而应该 return 一个序列。一个简单的方法是使用列表:
# 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
但它仍然不会遵循良好的封装实践,因为相同的函数显示消息并构建序列。如果 returned 列表的长度为 0 或 1,您应该只构建序列(可选的空列表)并让调用者决定显示什么。
如果您想更进一步,您应该考虑构建一个生成器,该生成器一次 yield
一个值,因此可以避免存储它们。
我想打印斐波那契数列的每第四项直到 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
我的第一个答案是垃圾,给你:
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)
注意:我写了一个答案,假设你的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中调用船长函数。船长函数调用序列函数,其中 returns 斐波那契数列。 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)
问题是您在最低级别使用 print
,因此无法从更高级别过滤任何内容。如果你想过滤,low fibo_sequence(n)
函数不应该打印任何东西,而应该 return 一个序列。一个简单的方法是使用列表:
# 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
但它仍然不会遵循良好的封装实践,因为相同的函数显示消息并构建序列。如果 returned 列表的长度为 0 或 1,您应该只构建序列(可选的空列表)并让调用者决定显示什么。
如果您想更进一步,您应该考虑构建一个生成器,该生成器一次 yield
一个值,因此可以避免存储它们。