Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Unsupported operand while adding 2 functions output in python?

I am using python 3.8.8 and while running a simple program of Fibonacci it is giving me an error i.e. unsupported operand type(s) for +: 'NoneType' and 'NoneType'

def fibo(n):
if n <= 1:
    return("Hey! Please enter value greater than 1")
else:
    return((fibo(n-1)) + fibo(n-2))

nterms = 10
res = fibo(nterms)
print(res)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Add a return n to your code

def fib(n):
    if n<=1:
        return n 
    else:
        return(fib(n-1) + fib(n-2))

for i in range(10):
    print(f"fib({i}) = " + str(fib(i)))

gives

fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading