1

I am using MVC (Module-View-Controller) pattern in my project. I want to keep my code DRY(Don't repeat yourself) so I don't want to write get_date function again just for another input.

I use function get_date() several times, but with different inputs. How can I pass these inputs to function get_date() so it could work?

Here I call get_date() function:

date = get_date(date_input = provide_date())

Here I want to pass the function with input message:

def get_date(date_input):
    ask = True
    while ask:
        date_input
        try:
            validation = datetime.strptime(date_input, '%Y-%m-
%d').strftime('%Y-%m-%d')
            date_list = validation.split('-')
            correct_date = 
date(int(date_list[0]),int(date_list[1]),int(date_list[2]))
            return correct_date
            ask = False
        except ValueError:
            wrong_input()

Provide_date function is:

def provide_date():
    return input('Type date in format Y-M-D: ')

I have another inputs for different situations like reschedule with another message.

How can I pass this input to get_date() function so it could work inside While loop? Cause now it loops forever when I raise TypeError.

2
  • 1
    The line that has just date_input does not do anything. Did you mean to pass date_input to the function (instead of date_input()) and then call date_input() on that line? The two are not equivalent. Commented Aug 10, 2017 at 13:59
  • @tobias_k yes, exactly. How should I do that? The input have to be called inside While loop to work properly. Commented Aug 10, 2017 at 14:08

1 Answer 1

1

There are a few problems with your code:

  • you call the function and then pass the result to get_date; instead, pass the function itself
  • then, call the function within get_date, and also bind the result to some variable
  • no need to parse the date, then format it, then parse it again, manually
  • since you return from the function, no need for the ask variable

Try this:

def get_date(date_input):
    while True:
        inpt = date_input()  # add () and bind result to variable
        try:
            dt = datetime.strptime(inpt, '%Y-%m-%d')  # no need to parse-format-parse
            return date(dt.year, dt.month, dt.day)  # use attributes of parsed datetime
        except ValueError:
            wrong_input()

print(get_date(provide_date))  # no () here
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @tobias_k for help! You are right. But the problem is, that I want to call get_date(date_input) function again, but with another input which is defined in function: def date_for_reschedule_event(): return input('Enter the date of the event you want to reschedule: ') How can I pass these functions with inputs to get_date() function, so I didn't have to write get_date() function again for another input?
@JeyKey I don't understand the question. Just pass the function (the function itself, not the result of calling the function), as I did in the answer, i.e. get_date(date_for_reschedule_event)
Sorry for that @tobias_k. I didn't see that you are calling that function in 3rd line of your code, that you passed. The name of that function misled me. Now everything works great ;) thank you for help!

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.