0

The below python function prints value using print function but is returning none if made to return string value. can anyone help me on how to return a string value from this below function.

csv_full_list = ['mycsv_0', 'mycsv_1']


def create_csv_name(comm, index):
    global csv_full_list
    if comm + '_' + str(index) in csv_full_list:
        index += 1 # increment index by 1
        create_csv_name(comm, index=index)
    else:
        print '%s_%i' % (comm, index)
        return '%s_%i' % (comm, index)


print(create_csv_name('mycsv', 0))

out put expected :

mycsv_2

but returns:

None

4
  • You don't return from the if Commented Nov 20, 2017 at 6:08
  • how does it prints value inside function then? Commented Nov 20, 2017 at 6:12
  • You should return from the recursive call: return create_csv_name(comm, index=index) Commented Nov 20, 2017 at 6:13
  • can you elaborate. I tried but getting error: RuntimeError: maximum recursion depth exceeded while getting the str of an object Commented Nov 20, 2017 at 6:30

2 Answers 2

0

Try out the following code.

csv_full_list = ['mycsv_0', 'mycsv_1']


def create_csv_name(comm, index):
    global csv_full_list
    result = True
    print "ATTEMPT: COMM: {}, INDEX: {}".format(comm, index)

    while result:
        if comm + '_' + str(index) not in csv_full_list:
            result = False

        else:
            index += 1 # increment index by 1
            #print "--> Found COMM: {}, INDEX: {}".format(comm, index)
            create_csv_name(comm, index=index)

    if not result:
        return "\n\nStopping at >> COMM: {}, INDEX: {}".format(comm, index)

# Call the def
print(create_csv_name('mycsv', 0))

2nd variation:

csv_full_list = ['mycsv_0', 'mycsv_1']


def create_csv_name(comm, index):
    global csv_full_list
    result = True
    print "ATTEMPT: COMM: {}, INDEX: {}".format(comm, index)

    if comm + '_' + str(index) not in csv_full_list:
        return "\n\nStopping at >> COMM: {}, INDEX: {}".format(comm, index)
    else:
        index += 1 # increment index by 1
        return create_csv_name(comm, index=index)

print(create_csv_name('mycsv', 0))

output:

> python.exe .\sample.py
ATTEMPT: COMM: mycsv, INDEX: 0
ATTEMPT: COMM: mycsv, INDEX: 1
ATTEMPT: COMM: mycsv, INDEX: 2
ATTEMPT: COMM: mycsv, INDEX: 2


Stopping at >> COMM: mycsv, INDEX: 2
Sign up to request clarification or add additional context in comments.

2 Comments

Quite honestly, I don't see why you need the while loop. It's an overkill to achieve the result.
@nightgaunt agreed, but wanted to keep it simple, so it could be understood easily. As pointed out by your comment the best practice would be to go for return create_csv_name(comm, index=index), Updated the code block. Thanks !!
0

You need to use return create_csv_name(comm, index=index) inside the function. Otherwise, you are not returning anything from the first call to the function. That also explains your None return value.

Recursion is a topic that requires keen observation. And takes some time to get familiar and apply in real application.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.