2017-10-15 4 views
-2

내가 뭘하려는 건지 정의 된 목록에서 선택 섹션을 표시하는 것 같아요. 현재이 내가 함께 일하고 것입니다 :fibonacci 시퀀스 파이썬 계산 프로그램

#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() 

내가 이것에 대해 이동하는 방법을 확실 해요,하지만 난 피보나치 순서로 89을 통해 3 (예를 들어) 표시하거나 뭔가를 할 노력하고있어 생각 like :

lsitOfFibSeq.remove(<3) and listOfFibSeq.remove(>89) 

또는 F 루프 시퀀스를 for 루프와 함께 표시해야합니까?

답변

0

사용자가 범위를 입력하기 전에 fibonacci 시퀀스를 미리 계산할 수있는 합리적인 방법이 없습니다. 동적으로해야합니다.

순진한 접근 방식은 주어진 (a, b)에 대한 시퀀스를 end까지 계산하고 start까지 모든 것을 버리는 기능을 갖는 것입니다.

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) 

또는 귀하의 경우, 뭔가 같은 :

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 

나는 발전기 접근 방식을 선호