为什么我的代码不能根据列表元素正确判断它们是否为质数?

Why my code doesn't deciding correctly from list elements that they are prime numbers or not?

我想确定这些数字是否为素数,如果它们被附加到一个新列表(在我的代码中称为素数)。我有一个 nums=[],随机填充的列表。

for i in range(len(nums)-1):
    for j in range(1,len(nums)-1):
        if(nums[i]%j==0):
            counter+=1
    if(counter==2):
        primes.append(nums[i])

这里有一个更清晰的方法,在除数上有一个上限。

primes = []
for num in nums:
    for divisor in range(2, int(num ** .5) + 1):
        if num % divisor == 0:
            break
    else:
        primes.append(num)