Home > OS >  Something wrong with the colon in a for loop (runtime error/ syntax error)
Something wrong with the colon in a for loop (runtime error/ syntax error)

Time:11-26

I am a coder in Leetcode, and it says I'm wrong with a colon on a for loop. This is my code:

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
    strnums = []
    for i in strnums: strnums  = str(i)
    for n1 in range(1, len(nums)   1):
        for n2 in range(1, len(nums) 1):
            if nums[n1] == nums[n2]
                pass
            elif n1   n2 == target:
                print(nums[n2])
            else:
                pass

there is something wrong with line 5 and 6,

will anyone help me?

CodePudding user response:

Line 4

for i in strnums: strnums  = str(i)

strnums is empty, this loop will not iterate.

Line 7

 if nums[n1] == nums[n2]

missing colon at the end of the if statement.

CodePudding user response:

Multiple colons are missing and the indentation is also missing in multiple places. Python is sensitive to indentation. The body of the for-loop, function, class, etc must be indented from its declaration:

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        strnums = []
        for i in strnums: 
            strnums  = str(i)
            for n1 in range(1, len(nums)   1):
                for n2 in range(1, len(nums) 1):
                if nums[n1] == nums[n2]:
                    pass
                elif n1   n2 == target:
                    print(nums[n2])
                else:
                    pass
  • Related