Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: 'set' object is not callable [PYTHON]

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading