1

** How can I print the return index1,index2? I try different ways but nothing was printed out. **

class Solution:
    def twoSum(self, nums, target):
        nums = [2,7,11,15]
        target = 9
        hash_map = {}
        for index, value in enumerate(nums):
            hash_map[value] = index
        for index1, value in enumerate(nums):
            if target - value in hash_map:
                index2 = hash_map[target - value]
                if index1 != index2:
                    return [index1,index2]
Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Mars_Tina
  • 33
  • 1
  • 10

1 Answers1

1
class Solution:
    def twoSum(self, nums, target):
        nums = [2,7,11,15]
        target = 9
        hash_map = {}
        for index, value in enumerate(nums):
            hash_map[value] = index
        for index1, value in enumerate(nums):
            if target - value in hash_map:
                index2 = hash_map[target - value]
                if index1 != index2:
                    return [index1,index2]

if __name__ == '__main__':
    print(Solution().twoSum(9, [2,7,11,15]))

I think what you are looking for is a main function in which you can use your function

rakwaht
  • 3,666
  • 3
  • 28
  • 45