AttributeError:“列表”对象没有属性“ drug_name”(类属性)

时间:2018-08-02 02:40:27

标签: python class typeerror

class Prescription:
    def __init__(self, patient, drug_name, quantity):
        self.patient = patient
        self.drug_name = drug_name
        self.quantity = quantity

    def __repr__(self):
        return 'Prescription(\'' + self.patient + '\', \'' + self.drug_name + 
        '\', ' + \
        str(self.quantity) + ')'

在另一个文件中,我有:

from lab10_classes import Pharmacy, Prescription


def batch_dispense(pharmacy, prescriptions):
    total_cost = 0
    if len(prescriptions) == 0:
        return 0
    for i in prescriptions:
        name = i.drug_name
        if name not in pharmacy.inventory:
            continue
        elif name in pharmacy.inventory:
            if pharmacy.inventory[name] > i.quantity:
                pharmacy.inventory[name] = pharmacy.inventory[name] - 
                prescriptions.quantity
                total_cost += pharmacy.unit_prices * prescriptions.quantity
    return total_cost and pharmacy


Print([Prescription('Mack', 'Zocor', -27), Prescription('Rachael', 'Crestor', 
      474), Prescription('Janet', 'Adderall', 60)])

在我目前正在研究的实验室中,我的一个功能参数称为“ prescriptions”,它接受Prescription对象的列表,例如上面的print语句。但是,我不断得到:

  

AttributeError:“列表”对象没有属性“ drug_name”

理论上,我的prescriptions参数应该有效,因为它具有Prescriptions类的实例类对象的列表。

0 个答案:

没有答案