1

I have constructed two functions. The first (not important but related since it is called in the second one) tells whether a number is a prime:

def is_prime(i):
    if i == 1:
        print("prime")
    if i == 2:
        print("not prime")
    for d in range(2, i):
        if i % d != 0:
            d = d+1
            if d == i:
                print('prime')
                break
        if i % d == 0:
            print('not prime')
            break

I want to be able to make this function count all the primes from 1 up till p. When I ask it to append it to the list - it returns an empty list and all the values individually.

def prime_counting(p):
    list_of_primes = []
    for n in range (p+1):
        if is_prime(n) == "prime":
            list_of_primes.append(n)

Instead of putting it in the list- it maps them separately

How can I resolve this?

Idos
  • 15,053
  • 14
  • 60
  • 75
Navy Seal
  • 125
  • 1
  • 14

2 Answers2

2

(Note: I didn't check the logic of your is_prime() function)
The glaring error in your code is that the function is_prime() does not return anything, it just prints. You want it to return True or False depending if the number is prime or not. Change it to do so.

Then you can check:

if is_prime(n) == True:
    list_of_primes.append(n)

Edit: as stated (correctly) in the comments, a more "pythonic" way of writing this kind of statement is:

if is_prime(n):
    list_of_primes.append(n)

Since is_prime() will return True or False (so there's no need to compare with them).

Idos
  • 15,053
  • 14
  • 60
  • 75
  • More pythonic is `if is_prime(n):` – GingerPlusPlus Feb 10 '16 at 08:06
  • If `is_prime(n)` returns `True` or `False`, do you need to compare that against `True`? Possibly it's enough to just use `if is_prime(n):` ...? – CiaPan Feb 10 '16 at 08:06
  • You are both right, since the OP is a beginner (I assume), I figured it would be a clearer approach. I will edit my answer with this though. Thanks – Idos Feb 10 '16 at 08:07
0

There are a few flaws in your code, but the one you are asking about is:

You are looking at the return value of is_prime() with

if is_prime(n) == "prime":

but is_prime() does not contain any return statements.

Here is a slightly modified version that actually returns a value from is_prime():

def is_prime(i):
    if i == 1:
        return "prime"
    if i == 2:
        return "not prime"

    for d in range(2, i):
        if i % d != 0:
            d = d + 1
            if d == i:
                return "prime"
        if i % d == 0:
            return "not prime"


def prime_counting(p):
    list_of_primes = []
    for n in range(p+1):
        if is_prime(n) == "prime":
            print("added %s" % n)
            list_of_primes.append(n)
    return list_of_primes


if __name__ == '__main__':
    print(prime_counting(3))

If you instead return a boolean value return True or return False you will be able to just do:

if is_prime(n):
   do_something()

or even use "comprehension" like so:

prime_list = [n for n in range(1, p+1) if is_prime(n)]

Idos
  • 15,053
  • 14
  • 60
  • 75
graN
  • 34
  • 6
  • 1
    While your main point is correct, it is *very bad practice* to `return` a string like that from a function. It is very error-prone (why "not prime" and not "Not prime" or "Not Prime" or "Not prime"?...) and should not be done. – Idos Feb 10 '16 at 08:05
  • I definitly agree and it is mentioned in the code that it in fact is bad practice. Though answering a question with code that is completely re-written may confuse more than it helps. – graN Feb 10 '16 at 08:15