6 Replies - 188 Views - Last Post: 18 January 2013 - 05:25 PM Rate Topic: -----

#1 farazamiruddin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 13

question about Recursion Function not returning proper output

Posted 18 January 2013 - 02:59 PM

I am having a problem with a function. The purpose of the function is to search through a large list for exact matches of strings. If the string ending matches one of the strings in the all_list, then that string is appended to name_list.

the problem is that once I find something, and the list is not empty, it should print "Found Something!" and return the list for use in other functions

if there is no match to the string being entered, then it should print "No File Found" and then ask the user again to input a different name. I did this by recursively calling the function.

If the user enters a correct file name the first time, then it works properly. But if the user enters a wrong file name, it will eventually find something but will return nothing

def search_by_name(all_list): #returns list of strings
    
    try:
        name_list = []
        name = str(input("\nEnter a file name: ")).strip()
        for i in all_list:
            if i[i.rfind("/")+1:] == name: 
                name_list.append(i)

        
        if name_list == []:
            print("No File Found")
            search_by_name(all_list)
            
        if name_list != []:
            print("\nFound Something!")
            

        return name_list

    except:
        search_by_name(all_list)




output:

Enter a file name: data.rtf

Found Something!

Is This A Good Question/Topic? 0
  • +

Replies To: question about Recursion Function not returning proper output

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 3,032
  • Joined: 21-June 11

Re: question about Recursion Function not returning proper output

Posted 18 January 2013 - 03:05 PM

Let's play through what happens:

name_list is empty so it goes in the if with the recursive call. That recursive call prints "Found something" and returns what it found, but you don't do anything with the return value. It then leaves the if and continues with return name_list. As we've just said, name_list is empty, so it returns the empty list. What you want to do is to return the list returned from the recursive call.

PS: A while loop would probably be a better choice than recursion here.
Was This Post Helpful? 0
  • +
  • -

#3 farazamiruddin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 13

Re: question about Recursion Function not returning proper output

Posted 18 January 2013 - 03:26 PM

the name _list starts off empty, but when it runs through the for loop shouldnt it have some values stored in it? eg. the name we were looking for?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 3,032
  • Joined: 21-June 11

Re: question about Recursion Function not returning proper output

Posted 18 January 2013 - 03:28 PM

After the loop the list may or may not be empty. However you only recurse if the list is empty, so that's the case I considered in my previous post.
Was This Post Helpful? 0
  • +
  • -

#5 farazamiruddin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 13

Re: question about Recursion Function not returning proper output

Posted 18 January 2013 - 03:57 PM

View Postsepp2k, on 18 January 2013 - 03:28 PM, said:

After the loop the list may or may not be empty. However you only recurse if the list is empty, so that's the case I considered in my previous post.


so If I add a return to the IF statement that says "Found Something" it should work correctly? How would I return the list that is being generated from the recursive call?
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2009
  • View blog
  • Posts: 3,032
  • Joined: 21-June 11

Re: question about Recursion Function not returning proper output

Posted 18 January 2013 - 04:20 PM

No, if you add a return to the if that says "Found nothing", it will work. If you find something (that is if name_list is not empty), returning the contents of name_list is the right thing to do - and you're doing that already.

Quote

How would I return the list that is being generated from the recursive call?


The same way you generally return the result of a function call (or any expression): you write return in front of the function call, i.e. return search_by_name(all_list).
Was This Post Helpful? 0
  • +
  • -

#7 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: question about Recursion Function not returning proper output

Posted 18 January 2013 - 05:25 PM

You are also checking if list empty and then if not empty using if statements. You can use if-else statement.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1