mercredi 18 novembre 2020

Using datetime with another function in Python

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:
            print("Wrong format")
    return a

b = test("Write date")

def func(date):
    if d('1/12/2020') <= d(date) <= d('1/12/2021'):
        print("ok")
    else:
        print("no")

func(b)

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?

Aucun commentaire:

Enregistrer un commentaire