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
>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)