My error-handling works, but something is wrong.
import datetime
def d(date):
return datetime.datetime.strptime(date, "%d/%m/%Y")
def test(prompt):
while True:
try:
value = input(prompt)
a = d(value)
break
except ValueError or TypeError:
print("Wrong format")
return a
b = test("Write date")
def func():
a = d(b)
if d('1/12/2020') <= a <= d('28/02/2021'):
print("ok")
else:
print("no")
func()
When I put b
in another function I get:
return datetime.datetime.strptime(datumet, "%d/%m/%Y")
TypeError: strptime() argument 1 must be str, not datetime.datetime.
But without error handling it works. What can be wrong with the code?