Question

In: Computer Science

The nutrition.py program (attached) creates a dictionary of food items (a mix of fruit and meat...

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

Solutions

Expert Solution

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


Related Solutions

3 perishables of food and the main reason for microbial growth. So meat, fruit, and milk,...
3 perishables of food and the main reason for microbial growth. So meat, fruit, and milk, why do microbes cause these to spoil. Talk about water availability, pH, oxygen, temperature
Please write in Python code please Write a program that creates a dictionary containing course numbers...
Please write in Python code please Write a program that creates a dictionary containing course numbers and the room numbers of the rooms where the courses meet. The dictionary should have the following key-value pairs: Course Number (key) Room Number (value) CS101 3004 CS102 4501 CS103 6755 NT110 1244 CM241 1411 The program should also create a dictionary containing course numbers and the names of the instructors that teach each course. The dictionary should have the following key-value pairs: Course...
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and...
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following: How to format each dictionary item as a text string in the input file. How to covert each input string into a dictionary item. How to format each item of your inverted dictionary as a text string in the output file. Create an input file with your original three-or-more...
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and...
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following: How to format each dictionary item as a text string in the input file. How to covert each input string into a dictionary item. How to format each item of your inverted dictionary as a text string in the output file. Create an input file with your original three-or-more...
Specification This script reads in class data from a file and creates a dictionary. It then...
Specification This script reads in class data from a file and creates a dictionary. It then prints the data from the dictionary. The script has the following functions class_tuple_create print_classes class_tuple_create This function has the following header def class_tuple_create(filename): The function reads in a file with four fields of data about a class. The fields are Course ID Room number Instructor Start time The function must print an error message if the file cannot be opened for any reason. The...
Why do items in a Python dictionary NOT stay in the order in which they are...
Why do items in a Python dictionary NOT stay in the order in which they are added? Put another way, how are the values indexed?
A meat baster consists of a squeeze bulb attached to a plastic tube. When the bulb...
A meat baster consists of a squeeze bulb attached to a plastic tube. When the bulb is squeezed and released, with the open end of the tube under the surface of the basting sauce, the sauce rises in the tube to a distance h, as the drawing shows. Using 1.013 × 105 Pa for the atmospheric pressure and 1470 kg/m3 for the density of the sauce, find the absolute pressure PB in the bulb when the distance h is (a)...
A farmer plans to mix two types of food to make a mix of low cost...
A farmer plans to mix two types of food to make a mix of low cost feed for the animals in his farm. A bag of food A costs $10 and contains 40 units of proteins, 20 units of minerals and 10 units of vitamins. A bag of food B costs $12 and contains 30 units of proteins, 20 units of minerals and 30 units of vitamins. How many bags of food A and B should the consumed by the...
In your home, choose five food items and five non-food items Make a list of these ten items and classify each item as:
In your home, choose five food items and five non-food items Make a list of these ten items and classify each item as: Made of cells or not made of cells? If not made of cells, of what is it made? If made of cells, are the cells dead or alive (in your best judgement)
A farmer can buy two types of plant food, mix A and mix B. Each cubic...
A farmer can buy two types of plant food, mix A and mix B. Each cubic yard of mix A contains 20 pounds of phosphoric acid, 30 pounds of nitrogen, and 5 pounds of potash. Each cubic yard of mix B contains 10 pounds of phosphoric acid, 30 pounds of nitrogen, and 10 pounds of potash. The minimum monthly requirements are 460 pounds of phosphoric acid, 960 pounds of nitrogen, and 220 pounds of potash. If mix A costs $30...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT