Recursive function returning none in Python
You need to return the recursive result:
else:
return get_path(directory[filename], rqfile, path)
otherwise the function simply ends after executing that statement, resulting in None
being returned.
You probably want to drop the else:
and always return at the end:
for filename in dictionary.keys():
path = prefix+[filename]
if not isinstance(dictionary[filename], dict):
if rqfile in str(os.path.join(*path)):
return str(os.path.join(*path))
return get_path(directory[filename], rqfile, path)
because if rqfile in str(os.path.join(*path))
is False
then you end your function without a return
as well. If recursing in that case is not the right option, but returning None
is not, you need to handle that edgecase too.
Recursive function returning none?
You are ignoring the return values of recursive calls. You need to explicitly return those too:
elif input[mid] > target:
return bisect(input[:mid], target)
elif input[mid] <= target:
return bisect(input[mid:], target)
Recursive calls are just like any other function call; they return a result to the caller. If you ignore the return value and the calling function then ends, you end up with that calling function then returning None
instead.
Why Recursive function return None
The return statement is missing!
Do this:
def good(num,sum):
if num == 0:
return sum
sum = sum + num
num = num - 1
return good(num, sum)
This is because the function is called but is not returning the new values recursively.
One of the best website that explains this in depth and clearly is realpython.com, have a look especially at: maintaining-state but I suggest you to have a look at the whole article.
For sake of completeness I quote a section where I think is related to the issue you encountered:
Behind the scenes, each recursive call adds a stack frame (containing its execution context) to the call stack until we reach the base case. Then, the stack begins to unwind as each call returns its results.
- unwind: Basically it returns it backward.
When dealing with recursive functions, keep in mind that each recursive call has its own execution context.
Other Stack Overflow Related questions
- recursive-function-returning-none-in-python
- python-recursion-with-list-returns-none
- i-expect-true-but-get-none
recursive function returning 'None'
Change it please to
def fib(n):
nums.append(nums[-1] + nums[-2])
if len(nums) - 1 != n:
return fib(n)
elif len(nums) - 1 == n:
return nums
Python recursive function returns None instead of value
You did not return any value from your recursive call break_up_number_into_fours(full_result)
, you need to do it like:
def break_up_number_into_fours(input):
flag = 0
input_list = []
full_result = []
if isinstance(input, list):
input_list.extend(input)
else:
input_list.append(input)
for item in input_list:
if item > 4:
result = [item/4]*4
full_result.extend(result)
flag = 1
else:
full_result.append(item)
if flag == 1:
return break_up_number_into_fours(full_result) // here you need to return the value in respective recursive call
else:
print(full_result)
return full_result
test = break_up_number_into_fours(20)
print(test)
Output:
[1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25]
[1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25, 1.25]
Python recursion with list returns None
You don't return anything in the else
clause:
else:
return foo(a)
Related Topics
String Concatenation of Two Pandas Columns
How to Get Monitor Resolution in Python
How to Round a Number to Significant Figures in Python
How to Select Rows in a Dataframe Between Two Values, in Python Pandas
Importerror: Dll Load Failed: %1 Is Not a Valid Win32 Application. But the Dll's Are There
Create Pandas Dataframe from Txt File with Specific Pattern
Python Analog of PHP's Natsort Function (Sort a List Using a "Natural Order" Algorithm)
How to Install Python3 Version of Package via Pip on Ubuntu
How to Calculate the Date Six Months from the Current Date Using the Datetime Python Module
How to Use Python to Login to a Webpage and Retrieve Cookies for Later Usage
Shared-Memory Objects in Multiprocessing
Return a Default Value If a Dictionary Key Is Not Available
Install a Module Using Pip for Specific Python Version
How to Use Xpath with Beautifulsoup
Store Different Datatypes in One Numpy Array