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

nothing returned in iterating Python function

Nothing is returned when I try iterating a function in Python.
Code is like this

def count(num):
    if (num > 10):
        return(num)
    if(num<=10):
        new = num + 1
        count(new)     
nummer = count(8)

If I do count(22), it returns 22. But when I do count(8), it doesnt return anything. I would like this function to return ’11’ but it return nothing.
Probably something wrong in my thinking but I really can’t figure it out.
I hope someone can help me.

Thx,
Peter

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

>Solution :

You actually need to include a second return, in case your number is lower or equal than 10. Besides, you could have a slightly shorter code. Calling the function is not sufficient.

You are trying to evaluated whether a number x is greater or lower than 10. But this number can EITHER be greater OR lower. Therefore when you put

if num>10:
  pass

you don’t need another if statement, since if num is not greater than 10 it is lower or equal to 10.

def count(num):
    if (num > 10):
        return(num)
    else:
        new = num + 1
        return count(new)     
nummer = count(8)
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