我是Python新手,我似乎无法用我的程序解决问题。
import datetime
class Pet:
def __init__ (self, name, year_born, species):
self.__name = name
self.__year_born = year_born
self.species = species
def __compute_age (self):
today = datetime.datetime.now()
return today.year - self.__year_born
def get_pet_details (self):
pet_details = {"Name" : self.__name, "species" : self.species, "Age" : self.__compute_age()}
return pet_details
class VetHospital:
def __init__ (self, name):
self.__name = name
self.__patients = []
def get_name (self):
return self.__name
def admit_patient (self):
pet_name_input = input('\n'"Please enter the pet/patient's name to be admitted: ")
pet_year_born_input = int(input("Please enter the {}'s year of birth: " .format(pet_name_input)))
pet_species_input = input("Please enter the {}'s species type: " .format(pet_name_input))
pet = Pet(pet_name_input, pet_year_born_input, pet_species_input)
pet.get_pet_details()
pet_details = {"Name" : pet_name_input, "species" : pet_species_input, "Age" : pet._Pet__compute_age()}
self.__patients.append(pet_details)
print('\n'"{} was successfully admitted into {}.".format(pet_name_input, self.__name))
def discharge_patient (self):
pet_name_input = input('\n'"Please enter the patient's name to be discharged: ")
for pet in self.__patients:
if pet._Pet__name == pet_name_input.lower():
self.__patients.remove(pet)
print("{} has been discharged.".format(pet_name_input))
return
print("There is no patient named {} admitted into {}.".format(pet_name_input, self.__name))
def get_patient_info (self):
pet_name_input = input('\n'"Please enter the patient's name: ")
for pet in self.__patients:
if pet.pet._Pet__name == pet_name_input.lower():
print(pet._Pet_get_pet_details())
return
print("There is no patient named {} admitted into {}.".format(pet_name_input, self.__name))
def get_all_patients_info (self):
all_patient_info = []
for pet in self.__patients:
all_patient_info.append(self.get_patient_info(pet))
if not self.__patients:
print('\n'"Currently, there are no patients admitted in {}." .format(self.__name))
else:
return all_patient_info
vet_hospital = VetHospital(input("Vet Name: "))
vet_hospital.admit_patient()
vet_hospital.discharge_patient()
vet_hospital_blank = VetHospital("h")
vet_hospital_blank.get_patient_info()
vet_hospital_blank.get_all_patients_info()
有两个问题:
pet_details = {"Name" : pet_name_input, "species" : pet_species_input, "Age" : pet._Pet__compute_age()}
我希望它与 Pet 类中的 pet_details 对象具有相同的属性,但我不知道如何使用 pet 实例调用 __compute_age 方法。