1

I'm a complete beginner to programming and I have recently completed a course on Intro to Python so I set out to practice my fundamentals on CodeWars. I wanted to use Recursion in the below problem but it kept returning None instead of the count. I want to know whats the error.

In a small town the population is p0 = 1000 at the beginning of a year. The population regularly increases by 2 percent per year and moreover 50 new inhabitants per year come to live in the town. How many years does the town need to see its population greater or equal to p = 1200 inhabitants?

My code is:

def nb_year(p0, percent, aug, p):
    global count
    if p0 >= p:
        return count 
    p0 = p0 + (p0*(percent/100))+aug
    count = count + 1
    nb_year(p0,percent,aug,p)

2 Answers2

0

Add return in the Recursive case.

def nb_year(p0, percent, aug, p):
    global count
    if p0 >= p:
        return count 
    p0 = p0 + (p0*(percent/100))+aug
    count = count + 1
    return nb_year(p0,percent,aug,p)
Luel
  • 16
  • 1
  • It looks like you have put your question in the title. Please include your question in the text of your post. – Donna Apr 21 '21 at 05:03
0

You actually didn't return anything. Therefore it prints none. To avoid that you can add a return statement to your code.

def nb_year(p0, percent, aug, p):
    global count
    if p0 >= p:
        return count 
    p0 = p0 + (p0*(percent/100))+aug
    count = count + 1
    return nb_year(p0,percent,aug,p) #this where you have to add the return statement
king juno
  • 11
  • 1
  • 4