0

I'm having a problem regarding the output of my function. Here are my codes:

def rrecursive(pop,n,r,K):
    if n==1:
        return pop
    else:
        popu=rrecursive(pop,n-1,r,K)
        y= popu+r*(1-popu/K)*popu
        if y>9.9:
            print("It will take",n*10,"years for the population to reach 9.9 billion")
            return 0
        return y
rrecursive(7,40,0.11,10)

All I'm trying to do here is display how many years It takes for the population to exceed 9.9 billion. My codes does work when pop is greater than 5 and n is greater than 40 but when it's below it prints nothing. If I call the function like rrecursive(5,40,0.11,10) it prints nothing. I tried using:

print(rrecursive(5,40,0.11,10))

it worked but when I plug print(rrecursive(7,40,0.11,10)) it prints out: output:

It will take 340 years for the population to reach 9.9 billion
0.0

I don't want the "0.0"

jonrsharpe
  • 107,083
  • 22
  • 201
  • 376
  • If you call the function it *does* print something. `print(rrecursive(7,40,0.11,10))` is explicitly asking for the return value, `y`, to be printed too. – jonrsharpe Mar 15 '21 at 17:51
  • Sorry I don't quite understand. Can you please elaborate more on it ?More on what I can do to improve my codes – Brandon Oson Mar 15 '21 at 18:27

0 Answers0