I need help fixing this python code so that it can display the output like shown below class FoodItem: def __init__(self): self.name = "Water" self.fat = 0.00 self.carbs = 0.00 self.protein = 0.00 def foodItem(self,name,fat,carbs,protein,servings): print(f"\nNutritional information per serving of {name}:") if name == self.name: print("Enter food name: Enter fat:") else: print(f'Fat: {fat}g') print(f'Carbohydrates: {carbs}g') print(f'Protein: {protein}g') calories = fat*9 + carbs*4 + protein*4 print(f'Number of calories for 1.00 serving(s): {calories}') print(f'Number of calories for {servings} serving(s): {servings*calories}') if __name__ == '__main__': name = input("Enter food name: ") fat = float(input("Enter fat: ")) carbs = float(input("Enter carbs: ")) protein = float(input("Enter protein: ")) servings = float(input("Enter number of servings: ")) food = FoodItem() food.foodItem(name,fat,carbs,protein,servings)
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
I need help fixing this python code so that it can display the output like shown below
class FoodItem:
def __init__(self):
self.name = "Water"
self.fat = 0.00
self.carbs = 0.00
self.protein = 0.00
def foodItem(self,name,fat,carbs,protein,servings):
print(f"\nNutritional information per serving of {name}:")
if name == self.name:
print("Enter food name: Enter fat:")
else:
print(f'Fat: {fat}g')
print(f'Carbohydrates: {carbs}g')
print(f'Protein: {protein}g')
calories = fat*9 + carbs*4 + protein*4
print(f'Number of calories for 1.00 serving(s): {calories}')
print(f'Number of calories for {servings} serving(s): {servings*calories}')
if __name__ == '__main__':
name = input("Enter food name: ")
fat = float(input("Enter fat: "))
carbs = float(input("Enter carbs: "))
protein = float(input("Enter protein: "))
servings = float(input("Enter number of servings: "))
food = FoodItem()
food.foodItem(name,fat,carbs,protein,servings)
![Input Water
Your output
Expected output
mpare output
Input
Your output
Enter food name: Enter fat:
Output differs. See highlights below. Special character legend
Expected output
Nutritional information per serving of Water:
Fat: 0.00 gel
Carbohydrates: 0.00 ge
Protein: 0.00 gel
Number of calories for 1.00 serving (s): 0.004
M&M's
10.0
34.0
2.0
3.0
Enter food name: Enter fat: Enter carbs: Enter protein: Enter number
Nutritional information per serving of M&M's:
Fat: 10.0g
Carbohydrates: 34.0g
Protein: 2.0g
Number of calories for 1.00 serving (s): 234.0
Number of calories for 3.0 serving (s): 702.0
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
Number of calories for 3.00 serving (s): 702.00](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fea56b2d3-976b-48ba-b1de-ef5a4d536379%2Fc38f1a13-95a6-45e5-b10a-f28208eac68e%2Fpik8s6a_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps with 3 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)