QUESTION

Text
Image

Help in Python, please! (:


main.py Load default template... class Fooditem: \# TODO: Define constructor with arguments to initialize instance \# attributes (name, fat, carbs, protein) def_init_(self, $n=$ "Water", $f=0.6, \quad c=0 . \theta, p=0 . \theta, s=0$ ): self, name $=n$ self.fat $=f$ self. carbohydrate $=c$ self. protein $=p$ self.serving $=s$ \#calculate calorie from the values self.calorie $=4^{*}($ self. protein+self. carbohydrate $)+9^{*}$ self.fat def get_calories(self, num_servings): \# Calorie formula calories $=(($ self.fat $* 9)+($ self.carbs $* 4)+($ self.protein * 4$))$ num_servings; return calories
item_name = input () if item_name $==$ 'Water' or item_name $==$ 'water': food_item = Fooditem () food_item.print_info() print $\left(f^{\prime}\right.$ Number of calories for $\{1.0: .2 f\}$ serving(s): \{food_item.get_calories(1.0):.2f\}') else: amount_fat $=$ float $($ input ()$)$ amount_carbs $=$ float $($ input ()$)$ amount_protein $=$ float $($ input ()$)$ num_servings $=f \operatorname{loat}(\operatorname{input}())$ food_item = FoodItem (item_name, amount_fat, amount_carbs, amount_protein) food_item.print_info() print (f'Number of calories for $\{1.0: .2 f\}$ serving(s): \{food_item.get_calories(1.0):.2f\}') print(f'Number of calories for \{num_servings:.2f\} serving(s): \{food_item.get_calories(num_servings):.2f\}')
9.18 LAB: Nutritional information (classes/constructors) Complete the FoodItem class by adding a constructor to initialize a food item. The constructor should initialize the name (a string) to "Water" and all other instance attributes to 0.0 by default. If the constructor is called with a food name, grams of fat, grams of carbohydrates, and grams of protein, the constructor should assign each instance attribute with the appropriate parameter value. The given program accepts as input a food item name, amount of fat, carbs, and protein, and the number of servings. The program creates a food item using the constructor parameters' default values and a food item using the input values. The program outputs the nutritional information and calories per serving for a food item. Ex: If the input is: Water the output is: Nutritional information per serving of Water: Fat: $0.00 \mathrm{~g}$ Carbohydrates: $0.00 \mathrm{~g}$ Protein: $0.00 \mathrm{~g}$ Number of calories for 1.00 serving(s): 0.00 Nutritional information per serving of Water: Fat: $0.00 \mathrm{~g}$ Carbohydrates: $0.00 \mathrm{~g}$ Protein: $0.00 \mathrm{~g}$ Number of calories for 1.00 serving(s): 0.00
1: Compare output Traceback (most recent call last): File "main.py", line 29, in <module> food_item.print_info() File "main.py", line 21, in print_info print (f' Carbohydrates: \{self.carbs:.2f $\mathrm{g}^{\prime}$ ) AttributeError: 'FoodItem' object has no attribute 'carbs' Output differs. See highlights below. Special character legend Input Water Your output Nutritional information per serving of Water: Fat: $0.00 \mathrm{~g}$ Nutritional information per serving of Water: Fat: $0.00 \mathrm{~g}$ Expected output Carbohydrates: $0.00 \mathrm{ge}$ Protein: $0.00 \mathrm{ge}$ Number of calories for 1.00 serving (s) : 0.00

Public Answer

NHLVCS The First Answerer