python - AttributeError:“list”对象没有属性“drug_name”(类属性)
问题描述
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)])
在我正在进行的当前实验室中,我的一个函数参数称为“处方”,它需要一个处方对象列表,例如上面的打印语句。但是,我不断得到:
AttributeError:“list”对象没有属性“drug_name”
从理论上讲,我的处方参数应该是有效的,因为它具有处方类的实例类对象列表。
解决方案
推荐阅读
- python - 如何计算Python中字符串列表中每个项目的出现次数?
- java - 在 Java FX 中,如何从左上角(而不是左下角)开始绘制文本?
- jquery - 解析 JSON 以在 JQuery 中创建二维数组
- javascript - 锚标签突然无法在 Wordpress 网站上运行
- javascript - 我有两个复选框“接受”和“拒绝”并提交按钮。当我单击提交按钮时,它应该检查勾选了哪个复选框
- python - 如何总结一天范围内的供应数量
- c++ - 错误:引用非静态成员函数
- powershell - 如何使@'...'@ 在 powershell.exe -command "" 中工作?
- c# - 如何在 C# 中设计 SMTP 邮件内容
- algorithm - kruskal算法生成的MST的一些性质问题