Look at the error message:
'str' object is not callable' error at this line -->
av=date(day, month, year).strftime('%d %B %Y')
You are calling two functions here:
date
and
strftime
so one of those is wrong.
Break the line in two and see if that helps:
xav=date(day, month, year)
av= xav.strftime('%d %B %Y')
Now you get the error on the first one:
xav=date(day, month, year)
So you at the surrounding code:
def date_fun(date):
try:
dt = parse(date)
...
xav=date(day, month, year)
date
is a parameter to the
date_fun
function, which means it overrides the function of the same name, so when you try to build a new date, it thinks you mean to use the variable.
Change the parameter name, and the error will go away:
def date_fun(datestr):
try:
dt = parse(datestr)
dt = dt.strftime('%d/%m/%Y')
dmy_split=re.split('[- / ]', dt)
day=int(dmy_split[0])
month=int(dmy_split[1])
year=int(dmy_split[2])
av=date(day, month, year).strftime('%d %B %Y')
if (day>=1 and day<=12 and month>=1 and month<=12 or len(year)==2):
print("Ambiguous; likely dates{}")
else:
print ("True,{}".format(av))
except ValueError:
print("False")
And please, sort out your indentation!
You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.
We all make mistakes.
And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!
So read this:
How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[
^] - it should help you next time you get a compilation error!