0

The function is returning a None after final print. What could be the cause?(please go easy on me,i am just a beginner.)

def prime(n):
    prime = [True for i in range(n+1)] #Boolean array for prime[0,n]
    p=2
    while (p*p<=n):
        if prime[p]==True: #Checking if marked or not
            for i in range(p*p,n+1,p):
                prime[i] = False #Marking multiples of p
    p+=1
    for p in range(2,n): #Getting values for true values
        if prime[p]:
           print(p)
print(prime(30))
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Croman
  • 56
  • 5
  • 3
    I would suggest trying to explain what you are trying to achieve. The function returns None most likely because you are trying to print the result of a function that returns nothing. – Nergon Nov 29 '19 at 14:23
  • 1
    In your case, you have to call `prime(30)`, without *print* word – karolch Nov 29 '19 at 14:24
  • May I introduce you to [rubber duck debugging](https://rubberduckdebugging.com/), I think this might solve your problem here ;) – Nearoo Nov 29 '19 at 14:25
  • One another issue may be the indentation used ```p+=1```. It should be inside the ```while``` loop. – a_r Nov 29 '19 at 14:41

1 Answers1

0

None is the default return value of a function (or the absence of it, because that's what None is).

shredding
  • 5,374
  • 3
  • 46
  • 77