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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
kapalkat
  • 396
  • 2
  • 4
  • 11
  • 2
    first you don't return anything from your `else` branch, second you shouldn't call your function recursively. Rewriting the routine with a loop would solve that. – Jean-François Fabre Feb 07 '17 at 13:48
  • 1
    See [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – roganjosh Feb 07 '17 at 13:49
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Łukasz Rogalski Feb 07 '17 at 13:55
  • your indentation is broken. Is all the code supposed to be part of `choose-sepa_reason_code`? – Bryan Oakley Feb 07 '17 at 14:23

2 Answers2

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)
WhatsThePoint
  • 3,395
  • 8
  • 31
  • 53
  • 1
    thanks, added the `while` loop and forgot to indent his code :) – WhatsThePoint Feb 07 '17 at 14:00
  • 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 – WhatsThePoint Feb 07 '17 at 14:03
  • 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](http://www.python-course.eu/recursive_functions.php) – roganjosh Feb 07 '17 at 14:03
  • 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 – WhatsThePoint Feb 07 '17 at 14:05
  • "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". – roganjosh Feb 07 '17 at 14:07
  • @kapalkat from your question it seemed like you wanted to loop until they enter a correct value? – WhatsThePoint Feb 07 '17 at 15:00
  • @WhatsThePoint that's partially true as at the first place I wanted to know why my function returns 'None'. However in final solution I ended up with your answer. – kapalkat Feb 07 '17 at 15:06
  • As the one you provided was not explaining why I was getting 'None' while executing the original function. – kapalkat Feb 07 '17 at 16:16
  • because the only time you are returning something is if they are right, and if they are wrong the function finishes anyway without returning anything – WhatsThePoint Feb 07 '17 at 16:19
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()
Danix
  • 61
  • 3