Prime Numbers python

前端 未结 5 880
长情又很酷
长情又很酷 2021-01-18 19:34

I\'m a beginning programmer in python and have a question about my code I\'m writing:

number = int(input(\"Enter a random number: \"))

for num in range(1, n         


        
5条回答
  •  有刺的猬
    2021-01-18 19:38

    I am assuming the random number is the range you want the numbers to be within.

    I found that the variable i is always equal to 2 in your code.This destroys the purpose of having a second for loop

    Prime numbers are numbers that cannot be divisible by 2, 3 or 7, excluding 2, 3 or 7!

    With this knowledge I adapted your code to show the true prime numbers.

    solution

    number = int(input("Enter a random number: "))
    
    for num in range(2,number +1) :
        printnum = True
        if num == 2:
            printnum = True
        elif num == 3:
            printnum = True
        elif num == 7:
            printnum = True
        elif num % 2 == 0:
            printnum = False
        elif num % 3 == 0:
            printnum = False
        elif num % 7 == 0:
            printnum = False
    
        if printnum == True:
            print(num)
    

提交回复
热议问题