Click here to Skip to main content
15,849,494 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For a day 11 challenge for Python I feel lik this one was extremely unfair. X(


https://www.hackerrank.com/challenges/30-2d-arrays/problem[^]



if __name__ == '__main__':
    arr = []
    max = -99
    for _ in range(6):
        arr.append(list(map(int, input().rstrip().split())))



sums = []
for i in range(4):
    for j in range(4):
        total = 0
        for k in range(3):
            for l in range(3):
                if k == 1 and l in [0, 2]:
                    continue
                total += arr[i+k][j+l]
        sums.append(total)

                            
print(max(sums))


I get an error:
Traceback (most recent call last):

  File "/tmp/submission/20220826/20/52/hackerrank-fb3d12eb5f6eb2b1d99475a9a96b5a15/code/Solution.py", line 29, in <module>

    print(max(sums))

TypeError: 'int' object is not callable



Works perfectly fine when I do this at the end instead:

print(sorted(sums)[-1])



My question is: Why on Earth am I not able to use the max function on the list in the above code without it throwing the above error?

What I have tried:

print(sorted(sums)[-1])
Posted
Updated 26-Aug-22 18:58pm

1 solution

Look at the error message:
File "/tmp/submission/20220826/20/52/hackerrank-fb3d12eb5f6eb2b1d99475a9a96b5a15/code/Solution.py", line 29, in <module>

    print(max(sums))

TypeError: 'int' object is not callable
So, the problem is with print, max, or sums.
You aren't calling anything on sums so we can ignore that.
print is a function: if we add a line above that which says
Python
print("Hello World")
It works and we get no error on that.
So it has to be a problem with max.
Look at the rest of your code, and we find this:
Python
max = -99
So you are defining max as an integer value. That overrides the "normal" definition of max as a function, and gives you a problem.
Remove the line, and it'll work: 56 is printed.

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!
 
Share this answer
 
Comments
Chris Aug2022 27-Aug-22 4:13am    
Thanks! Some reason I understood the error and I changed sum to Sumz and Sumzies(lol) but I completely overlooked 'max' as it was preset by the website to create the array and I had that function minimized! I'll check out the article when I wake up tomorrow as it's 4:12am. Thanks again.
OriginalGriff 27-Aug-22 4:53am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900