In: Computer Science
The nutrition.py program (attached) creates a dictionary of food items (a mix of fruit and meat items) and displays their corresponding information. The dictionary has the food item code, and Food object as key and value respectively. Write the missing code, in the designated locations, in order for the program execution to yield the following output when the input number of food items is 5:
Enter number of food items:
5
Type: Fruit
Category: Apples & Pears
Code: 1012030
Name: Fruit_0
Sugar: 10g
Type: Meat
Category: Chicken
Code: 1012031
Name: Meat_1
Protein: 21%
Type: Fruit
Category: Berries
Code: 1012032
Name: Fruit_2
Sugar: 12g
Type: Meat
Category: Beef
Code: 1012033
Name: Meat_3
Protein: 23%
Type: Fruit
Category: Citrus
Code: 1012034
Name: Fruit_4
Sugar: 14g
nutrition.py file
import random class Food: FRUIT_CATEGORIES =["Apples & Pears","Citrus","Berries"] MEAT_CATEGORIES = ["Beef","Chicken","Fish"] def __init__(self,code,name,category): self.code = code self.name = name self.category = category def info(self): print('\nType:',type(self).__name__) #TODO class Fruit(Food): # Add code for the __init__ function def info(self): super(Fruit,self).info() print('Sugar:',str(self.sugar)+"g") # Sugar content class Meat(Food): # Add code for the __init__ function def info(self): super(Meat, self).info() print('Protein:',str(self.protein)+"%") # Protein content # Enter the number of food items s = input("Enter number of food items:\n") n = int(s) foods = dict() for i in range(0,n): code = "101203"+str(i) rd = i % 3 if i % 2 == 0: food = Fruit(10+i%10,code,"Fruit_"+str(i),Fruit.FRUIT_CATEGORIES[rd]) else: food = Meat(20+i%10,code,"Meat_"+str(i),Fruit.MEAT_CATEGORIES[rd]) foods[code] = food keys = list(foods.keys()) keys.sort() for key in keys: foods[key].info()
Here is the completed python code for the above question.
Sometimes the code is not formatted properly, so I am attaching images of the code along with the output.
The line breaks can be modified for the output by using "\n" as per the requirement in the print statements.
import random
class Food:
FRUIT_CATEGORIES = ["Apples & Pears","Citrus","Berries"]
MEAT_CATEGORIES = ["Beef","Chicken","Fish"]
def __init__(self,code,name,category):
self.code = code
self.name = name
self.category = category
def info(self):
print('\nType:',type(self).__name__)
print('Category:',self.category)
print('Code:',self.code)
print('Name:',self.name)
class Fruit(Food):
def __init__(self, sugar, code, name, category):
self.sugar = sugar
super(Fruit,self).__init__(code,name,category)
def info(self):
super(Fruit,self).info()
print('Sugar:',str(self.sugar)+"g") # Sugar content
class Meat(Food):
def __init__(self, protein, code, name, category):
self.protein = protein
super(Meat,self).__init__(code,name,category)
def info(self):
super(Meat,self).info()
print('Protein:',str(self.protein)+"%") # Protein content
# Enter the number of food items
s = input("Enter number of food items:\n")
n = int(s)
foods = dict()
for i in range(0,n):
code = "101203"+str(i)
rd = i % 3
if i % 2 == 0:
food = Fruit(10+i%10,code,"Fruit_"+str(i),Food.FRUIT_CATEGORIES[rd])
else:
food = Meat(20+i%10,code,"Meat_"+str(i),Food.MEAT_CATEGORIES[rd])
foods[code] = food
keys = list(foods.keys())
keys.sort()
for key in keys:
foods[key].info()