In: Computer Science
Scenario
Suppose you are designing a piece of software for a packaging company. A part of the software involves an items capacity limit mechanism to prevent a package from being shipped when it's filled past its capacity.
Aim
Define a class called Package, which will have a maximum item capacity of 6.
Our aim here is to practice creating class attributes.
Steps for Completion
Go to you main.py file.
In the space provided, declare the Package class by adding an items limit class attribute.
Add the initializer, which will check whether the item limit will be exceeded, and print a message indicating how many items must be removed.
If the items limit has not been exceeded, print out the number of items in the package.
Finally, create a while loop that requests the user to input the number of items and then asks if we'd like to continue.
Create a few instances of the class declaration to test your implementation, Snippet 7.46 shows an example:
package1 = Package(6) print("There are", package1.items, "items in the package being shipped out.") package2 = Package(10) print("There are", package2.items, "items in the package being shipped out.")
Snippet 7.46
Run the script by running the python3 main.py command in the terminal. The output should look similar to Figure 7.3:
Figure 7.3
Grading
Complete each task listed below. Each task contains automated checks which are used to calculate your grade. When you have completed each task by clicking the checkbox, open the task list panel on the left navigation bar and click the "Submit" button.
Task
Define a class called Package that has a maximum capacity of 6 items and prints out a message indicating that the limit has been exceeded and how many items must be removed from the package.
The Python 3 Code is given below. Kindly copy the code and paste into the ide and run it to see the results. If you have any issues then comment in the comments section and kindly give a thumbs up if you liked the answer.
##################################################################
# Class Package Definition starts here #
##################################################################
class Package:
# Class attribute to store the maximum limit of
# the items that can be shipped in a Package
MAX_ITEM_CAPACITY = 6
# Function to initialize the number of items
# and to test for exceeding capacity of the items
def __init__(self, num_items):
self.__NumItems__ = 0 # attribute to store the number of items
# check if number of items to be shipped does not exceeds the limit
if(num_items > Package.MAX_ITEM_CAPACITY):
# if exceeds then calculate the difference
diff = num_items - Package.MAX_ITEM_CAPACITY
# print the messa ge and difference
print('The maximum item limit has been exceeded.')
print('%d items must be removed from the package.'%(diff))
# else print the shipping message
else:
self.__NumItems__ = num_items
print('There are %d items in the package being shipped out.'%(self.__NumItems__))
##################################################################
# Class Package Definition ends here #
##################################################################
# declare choice variable for inputting more items to be shipped
choice = 'y'
# loop untill choice is y or Y
while(choice == 'y' or choice == 'Y'):
# get number of items to be shipped
num_items = int(input('How many items are in the package? '))
# create a Package object sending it to, the number of items
# to be shipped
my_package = Package(num_items)
# ask whether to ship more or not
choice = input('Ship more packages?(Y/N) ')
Code Screenshot
CONSOLE OUTPUT
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
========== RESTART: C:/Program Workspace/Python 3 Workspace/Package.py =========
How many items are in the package? 7
The maximum item limit has been exceeded.
1 items must be removed from the package.
Ship more packages?(Y/N) y
How many items are in the package? 8
The maximum item limit has been exceeded.
2 items must be removed from the package.
Ship more packages?(Y/N) Y
How many items are in the package? 3
There are 3 items in the package being shipped out.
Ship more packages?(Y/N) N
>>>