Код: Выделить всё
import datetime
class Persona:
def __init__(self, nom, dni, fecha_nac):
self.nom=nom
self.dni=dni
self.fecha_nac=fecha_nac
def es_mayor_edad(self):
today=datetime.date.today()
cumple = datetime.datetime.strptime(self.fecha_nac, "%Y-%m-%d")
edad = today.year - cumple.year
if (today.month < cumple.month) or (today.month == cumple.month and today.day < cumple.day):
edad = edad - 1
if edad>=18:
return True
else:
return False
def __str__(self):
string = f"Nombre: {self.nom}\tDNI: {self.dni}\tFecha de nacimiento: {str(self.fecha_nac)}"
return string
Код: Выделить всё
from Ejercicio_4 import Persona
import datetime
nom=input("Introduce el nombre de la persona: ")
dni=input("Introduce el DNI de dicha persona: ")
fecha_nac_str=input("Introduce fecha nacimiento:[dd/mm/aaaa]: ")
fecha_nac= datetime.datetime.strptime(fecha_nac_str, "%d/%m/%Y")
persona=Persona(nom,dni,fecha_nac)
persona.es_mayor_edad()
print(persona)
if persona.es_mayor_edad()==True:
print(f"{persona.nom} es mayor de edad")
else:
print(f"{persona.nom} NO es mayor de edad")
Код: Выделить всё
File "C:\...\Ejercicio_4_main.py", line 10, in
persona.es_mayor_edad()
File "C:...\Ejercicio_4.py", line 12, in es_mayor_edad
cumple = datetime.datetime.strptime(self.fecha_nac, "%Y-%m-%d")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: strptime() argument 1 must be str, not datetime.datetime
Подробнее здесь: https://stackoverflow.com/questions/791 ... e-datetime
Мобильная версия