Prime numbers list generator immediately closing when run

前端 未结 2 843
余生分开走
余生分开走 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()
    
    0 讨论(0)
  • 2021-01-07 18:01

    The logic in your if-else is incorrect.

    If you get a "clean modulo" in your if block, what do you want to happen? Hint: not pass.

    Second, when do you want to append to your prime list?

    Not being too specific here as we shouldn't be doing this H/W assignment for you. :)

    0 讨论(0)
提交回复
热议问题