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 11:00
Ihave an iphone 8plus should i get another phone like samsung note 9 or s9 ? ?
Answers: 2
question
Computers and Technology, 23.06.2019 00:30
Which one of the following is the most accurate definition of technology? a electronic tools that improve functionality b electronic tools that provide entertainment or practical value c any type of tool that serves a practical function d any type of tool that enhances communication
Answers: 1
question
Computers and Technology, 23.06.2019 01:50
Write a program that uses a random number generator to generate a two digit positive integer and allows the user to perform one or more of the following operations: a. double the number. b. reverse the digits of the number. c. raise the number to the power of 2, 3, or 4. d. sum the digits of the number. e. if the number is a two-digit number, then raise the first digit to the power of the second digit. f. if the number is a three-digit number and the last digit is less than or equal to 4, then raise the first two digits to the power of the last digit. after performing an operation if the number is less than 10, add 10 to the number. also, after each operation determine if the number is prime. each successive operation should be performed on the number generated by the last operation. your program should not contain any global variables and each of these operations must be implemented by a separate function. also, your program should be menu driven. 7. (fraction calculator) write a program that
Answers: 1
question
Computers and Technology, 23.06.2019 22:30
The output voltage of a power supply is assumed to be normally distributed. sixteen observations are taken at random on voltage are as follows: 10.35, 9.30, 10.00, 9.96, 11.65, 12.00, 11.25, 9.58, 11.54, 9.95, 10.28, 8.37, 10.44, 9.25, 9.38, and 10.85
Answers: 1
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, 13.03.2021 01:50
question
English, 13.03.2021 01:50
question
Mathematics, 13.03.2021 01:50
question
Computers and Technology, 13.03.2021 01:50
question
Business, 13.03.2021 01:50
question
Mathematics, 13.03.2021 01:50
question
Mathematics, 13.03.2021 01:50