Prime numbers list generator immediately closing when run

前端 未结 2 865
余生分开走
余生分开走 2021-01-07 17:23

I\'m trying to make a list of prime numbers.

primes = []
num=int
for num in range (2,100):
    for x in range (2, num):
        if (num % x) == 0:
                   


        
2条回答
  •  暖寄归人
    2021-01-07 17:51

    Your code executes and finishes, but it does not compute a list of prime numbers because it contains an error:

    When you test each num to see if it's prime, you can test all possible divisors (as you try to do) and quit if you find a single divisor. If no divisor has been found when you've tested them all, only then add your number to the list

    also, the line num=int is not needed

    primes = []
    
    for num in range (2,100):
        is_prime=True
        for x in range (2, num):
            if (num % x) == 0:
                is_prime=False
                break
        if is_prime:
           primes.append(num)
    
    print(primes)
    input()
    

提交回复
热议问题