对允许0值时返回非0值的代码的处理建议

  • 本文关键字:代码 处理 返回 python python-3.x
  • 更新时间 :
  • 英文 :

class FoodItem:
def __init__(self,name,calories,serving_size=0,fat=0,carbs=0,protein=0):
self.name=name
self.calories=calories
self.serving_size=serving_size
self.fat=fat
self.carbs=carbs
self.protein=protein
@classmethod
def from_ingredients(cls,name,food_items):
calories=sum(item.calories for item in food_items)
serving_size=sum(item.serving_size for item in food_items)
fat=sum(item.fat for item in food_items)
carbs=sum(item.carbs for item in food_items)
protein=sum(item.protein for item in food_items)
return cls(name,calories,serving_size,fat,carbs,protein)
def print_properties(self):
print(f"{self.name}")
print("--------")
print(f"Calories: {self.calories}")
print(f"Serving Size: {self.serving_size}")
print(f"Fat: {self.fat}")
print(f"Carbs: {self.carbs}")
print(f"Protein: {self.protein}")
print("--------")
#  Name, calories, serving size, fat, carbs, protein: NCS-FCP
print("-------------------------------------------------------------------------")
#breakfast items
egg= FoodItem("Egg",72,50,5,0,6)
cheese= FoodItem("Cheese",100,24,7,0,5)
bacon= FoodItem("Bacon",45,8,3,0,3)
green_salsa= FoodItem("Green Salsa",45,8,3,0,3)
corn_tortilla= FoodItem("Corn Tortilla",130,28,5,17,2)
dkb_slice= FoodItem("DKB Slice",110,45,1,22,5)
peanut_butter= FoodItem("Peanut Butter",180,32,13,2,7)
butter=FoodItem("Butter",100,14,11,0,0)
*coffee=FoodItem("Coffee",5,8,0,0,0)*
bacon_grease=FoodItem("Bacon Grease",120,14,12,0,0)
of_oats=FoodItem("Old Fashioned Oats",140,40,2,27,5)
#WF Items and Snacks
wf_oat_protein_bites= FoodItem("WF Oat Protein Bites",160,40,7,23,8)
hu_kitchen_almonds=FoodItem("Hu Kitchen Chocolate Almonds",170,28,14,10,6)
wf_corn_tortilla= FoodItem("Corn Tortilla",130,28,5,17,2)
wf_coco_water=FoodItem("Whole Foods Coconut Water",100,8,0,26,0)
wf_rom_broc=FoodItem("Whole Foods Frozen Romanesco and Cauliflower",50,75,5,15,3)
wf_euro_greens=FoodItem("Whole Foods European Greens Vegetable Blend",90,110,6,8,3)
wf_spinach=FoodItem("Whole Foods Spinach",10,60,0,2,2)
quaker_sns_crisps=FoodItem("Quaker Sweet and Spicy Crisps",140,30,5,21,2)
reeses_mini_cups=FoodItem("Reese's Mini Cups",240,28,11,25,4)
pb_mms=FoodItem("Reese's Mini Cups",160,28,8,18,4)
protein_shake=FoodItem("Protein Shake",110,12,0,0,25)
fairlife_choc_milk=FoodItem("Chocolate Milk",140,8,5,13,13)
#Fruits
**apple=FoodItem("Apple",95,182,0.3,25,0.5)**
banana=FoodItem("Banana",105,118,0,27,1.3)
melon=FoodItem("Melon",186,552,1,45,4.6)
strawberry=FoodItem("Strawberry",47,144,0.4,11,1)
blueberry=FoodItem("Blueberry",85,148,0.5,21,1.1)
pineapple=FoodItem("Pineapple",83,166,0.2,22,1)
#Drinks
coffee=FoodItem("Coffee",95,182,0.3,25,0.5)
fairlife_choc_milk=FoodItem("Chocolate Milk",140,8,5,13,13)
protein_shake=FoodItem("Protein Shake",110,12,1,1,25)
green_tea=FoodItem("Green Tea",0,0,0,0,0)
diet_dr_pepper=FoodItem("Diet Dr Pepper",0,0,0,0,0)
wf_coco_water=FoodItem("Whole Foods Coconut Water",50,8,0,13,0)
#Meal Prep
fr_salmon=FoodItem("Farm Raised Salmon",300,180,31,0,25)
chkn_rice=FoodItem("Chicken and Rice",400,250,10,30,20)
ribeye=FoodItem("Ribeye",400,220,25,2,32)
sweet_potato=FoodItem("Sweet Potato",150,130,7,24,2)
filet_migon_6oz=FoodItem("Filet Mignon",450,250,30,0,45)
yellow_squash=FoodItem("Yellow Squash",75,170,5,12,3)
striped_bass=FoodItem("Striped Bass",250,170,16,0,26)
white_rice_150g=FoodItem("White Rice",200,150,0.5,45,4)
chkn_thigh_5oz=FoodItem("Chicken Thigh 5 oz",270,140,15,0,25)
#Takeout

print("-------------------------------------------------------------------------")

breakfast=FoodItem.from_ingredients("Breakfast",[coffee])
breakfast.print_properties()
lunch=FoodItem.from_ingredients("Lunch",[coffee])
lunch.print_properties()
dinner=FoodItem.from_ingredients("Dinner",[coffee])
dinner.print_properties()

EOD=FoodItem.from_ingredients("End of Day",[breakfast,lunch,dinner])
EOD.print_properties()

我运行脚本时,我注意到我把咖啡为所有三个值(早餐、午餐晚餐),我得到的宏和热量值苹果对于所有三个值作为输出。咖啡只有5卡路里和0宏。

为什么代码生成apple的值?而不是全部生成0

最后定义:

coffee=FoodItem("Coffee",95,182,0.3,25,0.5)

当你已经在更高层定义它时。

*coffee=FoodItem("Coffee",5,8,0,0,0)*

现在我不能100%确定这是否是错误输出的原因,但我假设它是。试着把饮料里的那条线去掉。

最新更新