Computers and Technology

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)))

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 09:00, starwarsfan1975
Meenu wants to create a high quality drawing in a variety of colours. which device should she use for the same?
Answers: 1
image
Computers and Technology, 24.06.2019 05:30, MegRasmussen31
Hey i really need some solving this problem: 1. encrypt this binary string into cipher text: 110000. include in your answer the formula the decoder would use to decrypt your cipher text in the format (coded answer) x n mod (m) = y & 2. decrypt this cipher text into a binary string: 106 you.
Answers: 2
image
Computers and Technology, 24.06.2019 13:00, CarlosParker99
Think of a spreadsheet as a giant calculator spread of paper chart data collector
Answers: 2
image
Computers and Technology, 24.06.2019 13:20, ana9340
3. ranga ramasesh is the operations manager for a firm that is trying to decide which one of four countries it should research for possible outsourcing providers. the first step is to select a country based on cultural risk factors, which are critical to eventual business success with the provider. ranga has reviewed outsourcing provider directories and found that the four countries in the table that follows have an ample number of providers from which they can choose. to aid in the country selection step, he has enlisted the aid of a cultural expert, john wang, who has provided ratings of the various criteria in the table. the resulting ratings are on a 1 to 10 scale, where 1 is a low risk and 10 is a high risk. john has also determined six criteria weightins: trust, with a weight of 0.3; quality, with 0.2; religious, with 0.1; individualism, with 0.2; time, with 0.1; and uncertainity, with 0.1. using the factor-rating method, which country should ranga select? why? (2 points)
Answers: 3
Do you know the correct answer?
IN PYTHON Complete the FoodItem class by adding a constructor to initialize a food item. The constr...

Questions in other subjects:

Konu
Mathematics, 18.03.2021 03:20