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

Why does my code produce error "'int' object is not iterable"

Here is the leetcode question: [Richest customer wealth][1]

and here is my solution:

class Solution:
    def maximumWealth(self, accounts: list[list[int]]) -> int:
        res = []
        for wealth in accounts:
            res = (sum(wealth))
        return max(res)

and I got the error such as:

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

TypeError: 'int' object is not iterable
    return max(res)
Line 6 in maximumWealth (Solution.py)
    ret = Solution().maximumWealth(param_1)
Line 25 in _driver (Solution.py)
    _driver()
Line 36 in <module> (Solution.py)

I simply converted this code into code above but I don’t know why this works and the another doesn’t?

class Solution:
    def maximumWealth(self, accounts: list[list[int]]) -> int:
        # 리스트 내포 사용
        accounts = [sum(wealth) for wealth in accounts]
        return max(accounts)

>Solution :

You want to add your wealth summary to a list and not replace it. So instead of

    res = []
    for wealth in accounts:
        res = (sum(wealth))

you want last line to look like

        res.append(sum(wealth))
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