self.food_list = [['apple', 100, 'Fruit', 384.0], ['orange', 100, 'Fruit', 384.0]]
我想对索引3中的元素求和。
预期输出为Total calorie of all food is 768.0Kcal
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))
但是出现了以下错误:
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
我想要做的是当用户想要总计列表中的所有卡路里时,系统将显示列表中的总卡路里。
还有其他办法吗?谢谢!
修改
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 += i[3]
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()
答案 0 :(得分:1)
在运行代码时我没有看到任何问题(但我不得不想象这个类,因为你没有给它)。
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()
输出:&#34;所有食物的总卡路里为768.0Kcal&#34;
PS:我使用的是Python2.7
编辑1:
正如Lukas Ansteeg在你的帖子的评论中所指出的那样,你的逻辑是j + = j&#34;
你应该这样做:
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))
编辑2:
现在我有了你的整个代码,我可以强调两个问题:
您正在尝试在Food对象上使用索引,但您的Food对象不支持对其构造方式进行索引,如果您确实要使用,请阅读this (from doc)或this支持索引的对象。
无论如何,您可以通过访问属性&#34; calorie&#34;轻松解决此问题。而是改为你的Food对象实例。因此j += i[3]
变为j += i.calorie
,但它不会起作用,因为(参见第2点)
输入返回一个字符串,因此i.calorie是一个字符串,你必须做j += float(i.calorie)
或者你会得到一个TypeError(Python,不像Javascript,不接受字符串和整数连接)
最后你的代码变成了这个:
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()
输出:
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
我希望我对此有所帮助但是,就像我在评论中所说的那样,你真的应该阅读一些关于OOP的好材料,比如&#34; Head First&#34;系列或者如果你喜欢更具互动性的东西,那么Udemy就有很棒的课程和教程。
答案 1 :(得分:1)
重复遍历food_list的元素。 作为对罗宾爵士的回答的改进:
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()
我也编辑了j +=
部分,因此它也适用于超过2个元素。