recursion - Recursive function returning none in Python -


this question has answer here:

i have piece of code, reason when try return path, none instead:

def get_path(dictionary, rqfile, prefix=[]):  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))      else:         get_path(directory[filename], rqfile, path) 

is there way solve this? in advance.

you need return recursive result:

else:    return get_path(directory[filename], rqfile, path) 

otherwise function ends after executing statement, resulting in none being returned.

you want drop else: , return @ 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)) false end function without return well. if recursing in case not right option, returning none not, need handle edgecase too.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -