subject

IN PYTHON Complete the FoodItem class by adding a constructor to initialize a food item. The constructor should initialize the name to "None" 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, 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 both food items.

Ex: If the input is:

M&M's
10.0
34.0
2.0
1.0
where M&M's is the food name, 10.0 is the grams of fat, 34.0 is the grams of carbohydrates, 2.0 is the grams of protein, and 1.0 is the number of servings, the output is:

Nutritional information per serving of None:
Fat: 0.00 g
Carbohydrates: 0.00 g
Protein: 0.00 g
Number of calories for 1.00 serving(s): 0.00

Nutritional information per serving of M&M's:
Fat: 10.00 g
Carbohydrates: 34.00 g
Protein: 2.00 g
Number of calories for 1.00 serving(s): 234.00

class FoodItem:
# TODO: Define constructor with parameters to initialize instance
# attributes (name, fat, carbs, protein)

def get_calories(self, num_servings):
# Calorie formula
calories = ((self. fat * 9) + (self. carbs * 4) + (self. protein * 4)) * num_servings;
return calories

def print_info(self):
print('Nutritional information per serving of {}:'.format(self. name))
print(' Fat: {:.2f} g'.format(self. fat))
print(' Carbohydrates: {:.2f} g'.format(self. carbs))
print(' Protein: {:.2f} g'.format(self. protein))

if __name__ == "__main__":

food_item1 = FoodItem()

item_name = input()
amount_fat = float(input())
amount_carbs = float(input())
amount_protein = float(input())

food_item2 = FoodItem(item_name, amount_fat, amount_carbs, amount_protein)

num_servings = float(input())

food_item1.print_info()
print('Number of calories for {:.2f} serving(s): {:.2f}'.format(num_servings,
food_item1.get_calories(num_serving s)))

print()

food_item2.print_info()
print('Number of calories for {:.2f} serving(s): {:.2f}'.format(num_servings,
food_item2.get_calories(num_serving s)))

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 16:10
Drag each label to the correct location on the imagelist the do’s and don’ts of safeguarding your password.keep yourself loggedin when you leave your computer.don’t write your password down and leave it whereothers can find it.share your password with your friends.each time you visit a website,retain the cookies on your computer.use a long password with mixed characters.
Answers: 1
question
Computers and Technology, 23.06.2019 03:10
Acomputer has a two-level cache. suppose that 60% of the memory references hit on the first level cache, 35% hit on the second level, and 5% miss. the access times are 5 nsec, 15 nsec, and 60 nsec, respectively, where the times for the level 2 cache and memory start counting at the moment it is known that they are needed (e.g., a level 2 cache access does not even start until the level 1 cache miss occurs). what is the average access time?
Answers: 1
question
Computers and Technology, 23.06.2019 09:30
Facial expressions and gestures are examples of messages.
Answers: 3
question
Computers and Technology, 23.06.2019 10:00
What is estimated time of arrival (eta)? a device that measures the acceleration (the rate of change of velocity) of an item and is used to track truck speeds or taxi cab speeds a gps technology adventure game that posts the longitude and latitude location for an item on the internet for users to find a north/south measurement of position the time of day of an expected arrival at a certain destination and is typically used for navigation applications
Answers: 3
You know the right answer?
IN PYTHON Complete the FoodItem class by adding a constructor to initialize a food item. The constr...
Questions
question
History, 10.10.2019 18:40
Questions on the website: 13722363