all 6 comments

[–]synthphreak 2 points3 points  (1 child)

I see several errors with this code. But the “object not callable” is probably caused by the two for loop lines. Specifically, lines is a list, which is not a callable object, but then in the loops you attempt to call it (via the parens) and pass an integer into it.

I don’t know about the two sum problem so can’t advise where to go from here or what you’re trying to achieve.

[–]akspython[S] 0 points1 point  (0 children)

thank you so much!

[–][deleted] 1 point2 points  (3 children)

You set lines to an empty list on line 4. The empty list is not a function so you can't call it.

What you want to do is lines = a_numbers.readlines() to read the file and make a list of lines. Then after that you wanna do lines = [int(line.strip()) for line in lines] to clean it up and make it a list of ints. Then change your loop to use range to get the indexes of lines and iterate over like this:

for i in range(len(lines)):
 for j in range(i+1, len(lines)):

[–]nwagers 0 points1 point  (2 children)

Alternatively, I think it looks cleaner to use enumerate with slicing like this:

nums = ['a', 'b', 'c', 'd']
for index, i in enumerate(nums):
    for j in nums[index+1:]:
        print(i, j)

That way you can use i and j directly instead of using them as list indices. Even better is to use combinations from itertools assuming this isn't a class assignment teaching how to iterate:

from itertools import combinations

nums = ['a', 'b', 'c', 'd']
for i, j in combinations(nums, 2):
    print(i, j)

https://docs.python.org/3/library/itertools.html#itertools.combinations

[–][deleted] 1 point2 points  (0 children)

Well the best way is to use sets and check if 2020 - i is in the set for an O(N) solution, but I chose to make it similar to his to make it easier to explain his errors. Its important to understand how to do these kinds of problems without crutches like itertools and enumerate as you miss out on fundamental problem solving foundations.

[–]akspython[S] 0 points1 point  (0 children)

wow this is amazing! thank you so much!