Lists, String Operations, and Functions
Assignment Background
In the previous two assignment you developed an ordering system and a sales analysis
program for an electronics store. For this assignment, you will use lists to create a more detailed
ordering system that will allow products to be added/removed from the stores’ catalogue, track
the stock levels of products, and provide the capability to search for products.
While you are free to store the data in any way you want, so long as the functions operate
correctly, the assignment description will use the suggested 2D list structure included below:
[[product#1 name, product#1 description, product#1 price, product#1 stock], [product#2
name, product#2 description, product#2 price, product#2 stock], etc.]
This structure consists of a product list that contains a single 4-item list to represent each
product’s information. As you will be using this list (or whatever storage mechanism you choose)
across most/all of the function in your program, it should be defined outside of any function
body.
You should use string operations to perform all of the searches and name comparisons in a
case-insensitive manner. This means that all of the following strings would be considered
matches: “Tablet”, “tablet”, “TABLET”, “TaBLeT”, etc..
All your code for this assignment should be placed in a file call a4.py. As with the previous
Note that you do not need to complete the functions in the order they are specified in this
document. If I were to recommend an order, I would implement the skeleton of the menu
function (problem 8) first and then add and test each of the additional functions in the following
order: load inventory (problem 1), list products (problem 7), add product (problem 3), save
inventory (problem 2), add product stock (problem 5), remove product (problem 4), sell product
(problem 6).
1
Problem 1 (Load Inventory Function)
Create a function called load_inventory(filename). The filename argument in this case specifies
the name of a file that contains all the inventory/product information for the store, including
product names, descriptions, prices, and stock levels. This function should clear any information
already in the product list (i.e., a fresh start) and then re-initialize the product list using the file
specified by the filename argument. You can structure your file any way you like, but a
suggested line-by-line structure like what was used in assignment #3 is given below:
Product #1 Name
Product #1 Description
Product #1 Price
Product #1 Stock
Product #2 Name
Product #2 Description
Product #2 Price
Product #2 Stock
…etc…
Problem 2 (Save Inventory Function)
Add another function to the a4.py file called save_inventory(filename). This function should save
all the product list information into the file with the specified name. It should also overwrite any
data that is already in the file. Once you have defined both the save and load inventory
functions, you will be able to load a stored information, perform some changes using the other
functions, then save those changes back to the file so they are remembered when the program
restarts and the file is loaded again.
Problem 3 (Add Product Function)
Add another function to the a4.py file called add_new_product(name, desc, price, stock). This
function accepts four arguments representing the information for a new product. It should add
that new product to the store’s product list only if no other product in the store’s product list has
the same name (case should be ignored, so “Tablet” and “tablet” would be considered the same
name). This will ensure the names of products in the store are unique, which will be important to
remove ambiguity in other functions (e.g., one that removes products by name). This function
must return True to indicate the product was successfully added or False to indicate the product
was not added.
2
Problem 4 (Remove Product Function)
Add another function to the a4.py file called remove_product(name). This function must go
through the store’s product list and remove a product from the list if that product’s name
matches the given name argument (again, this should not be case sensitive). The function
should return True to indicate a product was removed and False if a product was not removed.
Problem 5 (Add Product Stock Function)
Add another function to the a4.py file called add_product_stock(name, units). This function must
search through the store’s product list to find a product with the matching name. If a matching
product is found, the stock of that product in the store should be increased by the given number
of units. The function should return True to indicate the stock level of a product was updated
and False to indicate no updates were made (e.g., if a product was not found with that name).
Problem 6 (Sell Product Function)
Add another function to the a4.py file called sell_product(name, units). This function must
search through the store’s product list to find a product with the matching name. If a matching
product is found and the stock level of that product is at least the number of units specified (i.e.,
the store has that many units available to sell), then the stock level of that product should be
decreased by the specified number of units. This function must return True to indicate that a
product was found and the stock was updated. If a product with that name is not found or there
are not enough units in stock to complete the sale, the function must return False.
Problem 7 (List Products Function)
Add another function to the a4.py file called list_products(keyword). This function will search
through the list of products and print out those that match the given keyword argument. If the
keyword is “*”, then all products should be considered a match (essentially, this prints the entire
store inventory). Otherwise, a product should match the keyword if that keyword is contained
either in the product name or the product description (not case sensitive, “tablet” should match
“Tablet”, and “TABLET”, etc.). The printout should format the product information in an easy to
read fashion that includes the name, description, price and number of units in stock.
Problem 8 (System Menu)
Add a final function to the a4.py file called main(). This function must print out a menu with the 9
options detailed in the table below and allow the user to repeatedly enter choices until they
chose to quit. An example of what the output might look like is included on the assignment page
in the menu-demo.txt file.
3
Option Number
Option Text
Description
1
Load Inventory
Asks the user for a filename and then uses the
load_inventory function to perform the load operation.
2
Save Inventory
Asks the user for a filename and then uses the
save_inventory function to perform the save operation.
3
Add New Product
Ask the user to enter the four pieces of product
information and then use the add_new_product
function to add the product to the catalogue. Should
print out a message indicating whether the product was
added successfully or not (use the return value of the
function to decide).
4
Add Product Stock
Ask the user to enter the product name and a number
of units, then use the add_product_stock function to
update the stock levels of the product. Should print out
a message indicating whether the update was
successful or not (use the return value of the function to
decide).
5
List Products
Use the list_products function to print all the products in
the store catalogue.
6
Search Products
Ask the user to enter a keyword to search for. Use the
list_products function to print any matching products.
7
Sell Products
Ask the user for a product name and a number of units
to sell. Use the sell_product function to perform the sell
operation. Should print out a message indicating
whether the sale was successful or not (use the return
value of the function to decide).
8
Remove Products
Ask the user for a product name and then use the
remove_product function to take the product out of the
store inventory. Should print out a message indicating
whether the removal was successful or not (use the
return value of the function to decide).
9
Quit
Exits the loop and the program stops.
4
Desktop Computer
3Ghz, 16GB RAM, 250GB Hard Drive
1200
10
Laptop Computer
2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen
1500
15
This is example output created by the main() function when it is run. The
inventory.txt file that is loaded in this example is included on the assignment.
You can use it as a starting point for your own file or create your own by adding
products through the menu.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
There are no products that match.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>1
Enter the file name to load from:inventory.txt
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 10 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>3
Enter the product name: Tablet
Enter the product description: 9 inch tablet
Enter the product price: 750
Enter the starting stock amount: 12
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 10 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
3. Tablet, 9 inch tablet, $750.0, 12 units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>4
Enter the product name: tablet
Enter the number of units to add: 15
Product added successfully.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 10 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
3. Tablet, 9 inch tablet, $750.0, 27 units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>4
Enter the product name: iphone
Enter the number of units to add: 9
The product could not be added.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>6
Enter the keyword: RAM
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 10 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>7
Enter the product name: desktop computer
Enter the number of units: 8
Transaction successful.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 2 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
3. Tablet, 9 inch tablet, $750.0, 27 units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>7
Enter the product name: desktop computer
Enter the number of units: 3
The transaction could not be processed.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>8
Enter the product name: desktop computer
Product successfully removed.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
2. Tablet, 9 inch tablet, $750.0, 27 units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>2
Enter the file name to save to:inventory.txt
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>9
Total: 50 marks
Load Inventory Function (6 marks):
2 - Reads all information from file
4 - Stores product information correctly in list or similar data structure
Save Inventory Function (6 marks):
2 - Overwrites data present in file, if any
4 - Writes all information to file in correct pattern (matches load function
requirements)
Add New
2
2
2
Product Function (6 marks):
- Does not add product if matching name exists
- Adds product if no match found
- Correct return values
Add Product Stock Function (6 marks):
2 - Finds product correctly
2 - Updates stock correctly
2 - Correct return values
List Products Function (6 marks):
2 - Shows all products when "*" is input
2 - Shows only matching products when another string is input
2 - Nice formatting of output
Sell Products Function (6 marks):
2 - Finds product correctly
2 - Updates stock correctly
2 - Correct return values
Remove Products Function (6 marks):
2 - Finds product correctly
2 - Removes product if found
2 - Correct return values
Menu Function (8 marks):
1 mark for each of the 8 options (not including quit) that are connected to
the underlying functions correctly (ask for right information, call function, print
a message if required)
Desktop Computer
3Ghz, 16GB RAM, 250GB Hard Drive
1200
10
Laptop Computer
2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen
1500
15
This is example output created by the main() function when it is run. The
inventory.txt file that is loaded in this example is included on the assignment.
You can use it as a starting point for your own file or create your own by adding
products through the menu.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
There are no products that match.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>1
Enter the file name to load from:inventory.txt
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 10 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>3
Enter the product name: Tablet
Enter the product description: 9 inch tablet
Enter the product price: 750
Enter the starting stock amount: 12
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 10 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
3. Tablet, 9 inch tablet, $750.0, 12 units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>4
Enter the product name: tablet
Enter the number of units to add: 15
Product added successfully.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 10 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
3. Tablet, 9 inch tablet, $750.0, 27 units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>4
Enter the product name: iphone
Enter the number of units to add: 9
The product could not be added.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>6
Enter the keyword: RAM
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 10 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>7
Enter the product name: desktop computer
Enter the number of units: 8
Transaction successful.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Desktop Computer, 3Ghz, 16GB RAM, 250GB Hard Drive, $1200.0, 2 units in stock
2. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
3. Tablet, 9 inch tablet, $750.0, 27 units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>7
Enter the product name: desktop computer
Enter the number of units: 3
The transaction could not be processed.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>8
Enter the product name: desktop computer
Product successfully removed.
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>5
1. Laptop Computer, 2.5Ghz, 8GB RAM, 128GB Hard Drive, 15" Screen, $1500.0, 15
units in stock
2. Tablet, 9 inch tablet, $750.0, 27 units in stock
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>2
Enter the file name to save to:inventory.txt
Choose an option below to proceed:
1. Load Inventory
2. Save Inventory
3. Add New Product
4. Add Product Stock
5. List Products
6. Search Products
7. Sell Products
8. Remove Product
9. Quit
>9
Purchase answer to see full
attachment