0

My function in python looks like below:

def choose_sepa_reason_code():
    # This method is for providing proper sepa reason code

    sepa_reason_code = input("\nPlease provide SEPA reason code to reject the payment: ")

    if sepa_reason_code in sepa_reason_codes:
        return sepa_reason_code
    else:
        print("Your reason codes doesnt match the list:\n")
        pprint(sepa_reason_codes)
        choose_sepa_reason_code()

provided_sepa_reason_code = choose_sepa_reason_code()

The if statement is to make sure that user will provide proper code, but if he is wrong at the first time my function later returns None.

Could you please advise how to change it to get final correct user input?

4

2 Answers 2

1

If you really want to call the function recursively, call choose_sepa_reason_code() as return value in your else statement:

sepa_reason_code = input("\nPlease provide SEPA reason code to reject the payment: ")

if sepa_reason_code in sepa_reason_codes:
    return sepa_reason_code
else:
    print("Your reason codes doesnt match the list:\n")
    print(sepa_reason_codes)
    return choose_sepa_reason_code()
Sign up to request clarification or add additional context in comments.

3 Comments

wasnt the whole point in him recalling the function because he wanted it to run until the user enters correctly?
@WhatsThePoint which is what this does.
@WhatsThePoint Indeed, I would also prefer a solution with a loop, but the OP question was about a recursive one.
1

firstly your code wont run because it isnt indented, secondly you cant call a function inside itself.

this would be a better approach to your task:

def choose_sepa_reason_code():
# This method is for providing proper sepa reason code
    while True: #will provide permanent loop
        sepa_reason_code = input("\nPlease provide SEPA reason code to reject the payment: ")

        if sepa_reason_code in sepa_reason_codes:
            return sepa_reason_code #stops loop and returns the reason code
        else:
            print("Your reason codes doesnt match the list:\n")
            print(sepa_reason_codes)

9 Comments

thanks, added the while loop and forgot to indent his code :)
also side note to the OP your names of variables, function etc are too similar just with minor differences, makes it harder to read, i would recommend changing them
No prob. A point of note though. You write "secondly you cant call a function inside itself." but this is not true. See this link about recursion
yeah i know technically you can but in most cases you shouldnt because it just starts itself again executes and finishes the original, which isnt a very good approach
"Can't" and "but in most cases you shouldn't" are different things :) The title of the post is quite generic for someone who might be Googling the subject and they may well come across your answer which states it "can't be done".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.