I am trying to write a simple module to store date data from the information entered by the user:
import datetime
formato_dia = ("%Y%m%d","%Y/%m/%d", "%Y-%m-%d")
def dia_manual():
while True:
entrada_dia = input("""Escriba la fecha para la que desea pedir cita previa
(AAAAMMDD, AAAA/MM/DD ó AAAA-MM-DD):
""")
try:
for i in formato_dia:
try:
return(datetime.datetime.strptime(entrada_dia, i))
break
except:
continue
break
except:
print("No se ha introducido una fecha con un formato válido. Por favor, inténtelo de nuevo.")
continue
print("Ha solicitado una cita previa para el día: "+str(dia_manual()))
I would need to know:
dia_manual()
is also correct, that is, an object is generated
datetime.date
containing the information entered. But
enter any other value or an incorrect date, it is not generated
an exception, which should be followed by a new request from the
information to the user. Instead of that exception,
datetime.strptime
returns a NoneType
object. Why?. strftime
. How can I know
what format did strptime
use to pass it as an argument? Thank you very much in advance.