fibonacci序列Python的计数程序
我想我要做的是从定义的列表中显示一个select部分。目前,我正在处理以下问题:fibonacci序列Python的计数程序,python,python-3.x,append,fibonacci,Python,Python 3.x,Append,Fibonacci,我想我要做的是从定义的列表中显示一个select部分。目前,我正在处理以下问题: #fibonacci sequence algorithm, user stops by either #entering a maximum Fibonacci value not to exceed or #a total count that the sequence of numbers must not #exceed. Use a loop that allows User to repeat pro
#fibonacci sequence algorithm, user stops by either
#entering a maximum Fibonacci value not to exceed or
#a total count that the sequence of numbers must not
#exceed. Use a loop that allows User to repeat program
#as much as they wish, asking if they would like to
#repeat the program each time. Validate that User input
#is either a yes or a no and only allow User to continue
#once a correct response has been given.
import array
array.listOfFibSeq = ['0','1','1','2','3','5','8','13','21','34','55','89','144','...']
startingNumber = ''
endingNumber = ''
continueYes = ''
def getStartingNumber():
print('Please enter a valid starting number of the Fibonacci Sequence')
print(listOfFibSeq)
startingNumber = input()
def getEndingNumber():
print('Please enter a valid ending number the the Fibonacci Sequence')
print(listOfFibSeq)
endingNumber = input()
我不确定该怎么做,但我相信我正在尝试在斐波那契序列中显示(例如)3到89,或者做类似的事情:
lsitOfFibSeq.remove(<3) and listOfFibSeq.remove(>89)
start = input('Start index: ')
end = input('End index: ')
for i in itertools.islice(fib(), int(start), int(end)):
print(i)
if input('Continue [y/n]: ').rstrip() == 'n':
break
lsitOfFibSeq.移除(89)
或者我应该尝试用for循环来显示Fib序列的范围吗?在用户输入范围之前,没有合理的方法来预计算fibonacci序列-您应该动态地这样做 一种简单的方法是使用一个函数来计算给定
(A,b)
的序列,一直到结束
,放弃所有直到开始
我更喜欢生成器方法:
import itertools
def fib():
a, b = 0, 1
while 1:
yield a
a, b = b, a + b
# Print the first 10 values of the sequence
for i in itertools.islice(fib(), 0, 10):
print(i)
或者,在您的情况下,类似于:
lsitOfFibSeq.remove(<3) and listOfFibSeq.remove(>89)
start = input('Start index: ')
end = input('End index: ')
for i in itertools.islice(fib(), int(start), int(end)):
print(i)
if input('Continue [y/n]: ').rstrip() == 'n':
break