viewed some other forums to see if I could find an answer to this but I didn’t find anything satisfactory. I’m writing a program to work out the fibonacci sequence up to a specified index, but I keep getting an error.
allNums = {1, 1}
while len(allNums) < 35:
currentIndex = len(allNums)
nextIndex = allNums[currentIndex - 1] + allNums[currentIndex - 2]
allNums[currentIndex] = nextIndex
print(nextIndex)
This is working fine on my laptop, but for some reason when I try it on my desktop it gives me the error:
nextIndex = allNums(currentIndex - 1) + allNums(currentIndex - 2)
TypeError: 'set' object is not callable
Any help?
>Solution :
Sets do not support calling allNums(currentIndex - 1)
nor are subscriptable (i.e allow index assigning) allNums[currentIndex - 1]
. There are designed for quick search of items, not storing them in order.
You need allNums
to be a list, not a set:
allNums = [1, 1]
while len(allNums) < 35:
currentIndex = len(allNums)
nextIndex = allNums[currentIndex - 1] + allNums[currentIndex - 2]
allNums.append(nextIndex)
print(nextIndex)
Output:
2
3
5
8
13
21
34
...