Prime Numbers python

前端 未结 5 889
长情又很酷
长情又很酷 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:55

    the trouble is in your else: statement which has to run outside of the first loop. I recommend the following:

    def isprime(num):
        for i in range(2, num):
            if (num % i) == 0:
                return False
        return True
    
    
    for num in range(1, number + 1) :
        if isprime(num):
            print num
    

提交回复
热议问题