I think the problem there is that on the first go in that loop your list is empty, so fib[-1] will not refer to anything on the first pass.
You could put a test in for that condition that would test to make sure the list has at least one item.
fib_list=[]
highest_value =10
n = 0
while True
___ if len(fib_list) > 0 and fib_list[-1] < highest_value:
_______ fib_list.append(Fibonacci(n)) #Fibonacci() defined earlier
________n += 1
___ elif len(fib_list) = 0:
_______ fib_list.append(n)
___ if n >= highest_value:
_______ break
I recently read a function that someone wrote to handle this kind of problem. Its in the Python documentation..for the functools module in the part about the lru_cache. They have a recursive function.
def fib(n):
___ if n < 2:
_______ return n
___ return fib(n-1) + fib(n-2)
Its short and works!
Sorry for the delayed response. I actually read your response yesterday, but it was getting close to the Sabbath and I had to run into the shower and afterwards it was too late to respond.
I fixed the problem by adding a 0 to the list, based on your suggestion. So I guess yesterday I learned that nothing !< 10. Just goes to show.
I ended up having to change it altogether, because the way I had it before, I was appending the number right over my target. So now I'm using:
while True:
if fib_list[-1] < highest_value:
fib_list.append(Fibonacci(n))
n+=1
else:
continue
I'm actually using a recipe I found that's not too different although yours is a little shorter:
def Fibonacci(n):
if n<0:
print("Incorrect input")
elif n==0:
return 0
elif n==1:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
Math makes me dizzy, so I don't really care what it does, as long as it works!