Home > Software design >  How to solve TypeError: 'int' object is not iterable in Python while calculating sum of tw
How to solve TypeError: 'int' object is not iterable in Python while calculating sum of tw

Time:11-24

I am trying to take two values as parameters and return True if its value is equal to 10 and false if it isn't. The values are strictly int. Here is the code

    class Solution:
    def twomakes10(self, no1, no2):

        if sum(no1, no2) == 10:
            return True
        else:
            return False


if __name__ == "__main__":
    p = Solution()
    n1 = 9
    n2 = 1
    print(p.twomakes10(n1, n2))

CodePudding user response:

sum function, gets an iterable value as input, you can try:

sum([no1,no2])

CodePudding user response:

sum is expecting iterable .. so make it happy. See below

class Solution:

    def twomakes10(self, no1, no2):
        return sum([no1, no2])


if __name__ == "__main__":
    p = Solution()
    n1 = 9
    n2 = 1
    print(p.twomakes10(n1, n2))

CodePudding user response:

TypeError: ‘int' Object is Not Iterable - Python Pool So, you have encountered the exception, i.e., TypeError: ‘int' object is not iterable. For instance, standard python iterable are list, tuple, string, and dictionaries.

  • Related