Python 类型错误:';食品';对象不支持索引
我想对索引3中的元素求和。 预计产量为所有食品的总热量为768.0KcalPython 类型错误:';食品';对象不支持索引,python,python-3.x,Python,Python 3.x,我想对索引3中的元素求和。 预计产量为所有食品的总热量为768.0Kcal self.food_list = [['apple', 100, 'Fruit', 384.0], ['orange', 100, 'Fruit', 384.0]] 但出现了以下错误: def totalCalorie(self): for i in range(0, len(self.food_list)): j = self.food_list[i][3] j += j
self.food_list = [['apple', 100, 'Fruit', 384.0], ['orange', 100, 'Fruit', 384.0]]
但出现了以下错误:
def totalCalorie(self):
for i in range(0, len(self.food_list)):
j = self.food_list[i][3]
j += j
print("Total calorie of all food is {}Kcal".format(j))
在运行代码时,我看不到任何问题(但是我不得不想象这个类,因为您没有给出它) 输出:“所有食物的总热量为768.0Kcal” 附言:我用的是Python2.7 编辑1:
Traceback (most recent call last):
File "D:/PythonTim/Assignment/nutritionDriver.py", line 60, in <module>
main()
File "D:/PythonTim/Assignment/nutritionDriver.py", line 43, in main
patient.totalCalorie()
File "D:\PythonTim\Assignment\nutritionApp.py", line 132, in totalCalorie
j = self.food_list[i][3]
TypeError: 'Food' object does not support indexing
正如卢卡斯·安斯蒂格(Lukas Ansteeg)在你的帖子评论中指出的,“你的j+=j的逻辑存在缺陷”
您应该这样做:
class Food:
def __init__(self):
self.food_list = None
def totalCalorie(self):
for i in range(0, len(self.food_list)):
j = self.food_list[i][3]
j += j
print("Total calorie of all food is {}Kcal".format(j))
f = Food()
f.food_list = [['apple', 100, 'Fruit', 384.0], ['orange', 100, 'Fruit', 384.0]]
f.totalCalorie()
编辑2:
Traceback (most recent call last):
File "D:/PythonTim/Assignment/nutritionDriver.py", line 60, in <module>
main()
File "D:/PythonTim/Assignment/nutritionDriver.py", line 43, in main
patient.totalCalorie()
File "D:\PythonTim\Assignment\nutritionApp.py", line 132, in totalCalorie
j = self.food_list[i][3]
TypeError: 'Food' object does not support indexing
现在我有了您的全部代码,我可以强调两个问题:
j+=i[3]
变成了j+=i.carorie
,但它不起作用,因为(见第2点)j+=float(i.calorie)
,否则您将得到一个TypeError(Python与Javascript不同,不接受字符串和整数连接)class Food:
def __init__(self):
self.food_list = None
def totalCalorie(self):
total = 0
for i in range(0, len(self.food_list)):
j = self.food_list[i][3]
total += j
print("Total calorie of all food is {}Kcal".format(total))
输出:
class Food:
def __init__(self, name, quantity, category, calorie):
self.name = name
self.quantity = quantity
self.category = category
self.calorie = calorie
class FoodList:
def __init__(self):
self.food_list = []
def totalCalorie(self):
j = 0
for i in self.food_list:
j += float(i.calorie)
print("Total calorie of all food is {}Kcal".format(j))
def addFood(self, newfruit):
self.food_list.append(newfruit)
name = input("Name? ")
quantity = input("Quantity? ")
category = input("Category? ")
calorie = input("Calorie? ")
f = Food(name, quantity, category, calorie)
patient = FoodList()
patient.addFood(f)
name1 = input("Name? ")
quantity1 = input("Quantity? ")
category1 = input("Category? ")
calorie1 = input("Calorie? ")
f1 = Food(name1, quantity1, category1, calorie1)
patient.addFood(f1)
patient.totalCalorie()
我希望我在这方面有所帮助,但是,正如我在评论中所说的,你真的应该读一些关于OOP的好材料,比如“Head First”系列,或者如果你喜欢更具互动性的东西,Udemy上有很多很棒的课程和教程。只要重复一下食物列表中的元素就可以了。 作为对罗宾爵士答案的改进:
Name? apple
Quantity? 100
Category? Fruit
Calorie? 384.0
Name? Coq_au_vin
Quantity? 1
Category? Delicious_meal
Calorie? 142000.0
Total calorie of all food is 142384.0Kcal
我也编辑了
j+=
部分,这样它也可以处理2个以上的元素。你确定self.food\u列表具有你期望的值吗?当我复制你的代码时,它就工作了。(你的j+=j的逻辑有一个缺陷,但这并没有导致错误)请提交整个类当你尝试打印type(self.food_列表)时会发生什么?@MohammadAthar我把self.food_列表=[]放在FoodList构造函数中,所以我尝试了type(FoodList.food_列表)但是得出的结论是:AttributeError:type对象“FoodList”没有属性“food_list”self.food_list是一个基于实例的对象(也就是说,它是在创建类的实例时创建的),它不同于FoodList.food_list。在self.food_list中为我做“for i”不是更容易吗?@Leonhard Triendl->绝对如此,但为了清晰起见,我想让他的代码基本上保持不变;)在我的代码中,类构造函数具有self.food_list=[]@OysterD3。您可以编辑您的原始帖子以包含完整的代码吗?像这样理解你的问题会容易得多。@SirRobin我不能,它说即使我缩短了帖子中的代码,也会有太多代码,有没有其他方法发送给你?如果列表中的元素是用户输入的呢?当用户调用addFood方法时,它将追加到列表中。我无法在构造函数中使其成为food\u list=None。这会给我带来另一个错误。我编辑了我的答案,以便你可以添加你喜欢的食物。你的答案对我帮助很大,我发现错误发生在我的另一堂课上。我已经更新了,请检查。提前谢谢!
class Food:
food_list = []
def totalCalorie(self):
j=0
for i in self.food_list:
j += i[3]
print("Total calorie of all food is {}Kcal".format(j))
def addFood(self,newfruit):
self.food_list.append(newfruit)
f = Food()
f.addFood(['banana', 100, 'Fruit', 100.0])
f.addFood(['orange', 100, 'Fruit', 384.0])
f.totalCalorie()