Python代写:PA371 Guido's Grocery Database

用Python实现一个简单的数据库,用List即可。

Requirement

For this week’s programming assignment, pretend you have been hired by a small grocery store called Guido’s Grocery to create a table for their grocery store database. The table will store information about all of its products. In addition to creating the table, you will create a few functions that interact with the table. You will also create a user interface that makes it easy for any user to run and interact with your code.

The focus of the programming assignment is working with 2D lists. You will also practice writing functions and using a while loop and conditional statements to create a useful user interface.

Example

Below is an example of what the shell should look like when you run your program. Note that the text in black is what the user typed in on their keyboard.

Welcome to Guido's Grocery's Item Database

Enter 1 to display the table of our products
Enter 2 to add a product
Enter 3 to caclulate the average cost of the items
Enter 4 to caclulate the total number of individual items
Enter 0 to exit the program
What would you like to do? 1

Product Name   UPC          Price       Number in Stock
Milk           95520        3.27        20
Eggs           55504        2.97        15
Bread          57971        2.78        20
Apples         19791        0.78        70
Cheese Bits    32510        2.99        25
Cheese Bytes   84159        23.92       10

Enter 1 to display the table of our products
Enter 2 to add a product
Enter 3 to caclulate the average cost of the items
Enter 4 to caclulate the total number of individual items
Enter 0 to exit the program
What would you like to do? 3

The average cost of the items in stock is 6.118333333333333

Enter 1 to display the table of our products
Enter 2 to add a product
Enter 3 to caclulate the average cost of the items
Enter 4 to caclulate the total number of individual items
Enter 0 to exit the program
What would you like to do? 4

The total number of items currently in stock is 160

Enter 1 to display the table of our products
Enter 2 to add a product
Enter 3 to caclulate the average cost of the items
Enter 4 to caclulate the total number of individual items
Enter 0 to exit the program
What would you like to do? 0

Here is another example of a sample run:

Welcome to Guido's Grocery's Item Database

Enter 1 to display the table of our products
Enter 2 to add a product
Enter 3 to caclulate the average cost of the items
Enter 4 to caclulate the total number of individual items
Enter 0 to exit the program
What would you like to do? 1

Product Name   UPC          Price       Number in Stock
Milk           95520        3.27        20
Eggs           55504        2.97        15
Bread          57971        2.78        20
Apples         19791        0.78        70
Cheese Bits    32510        2.99        25
Cheese Bytes   84159        23.92       10

Enter 1 to display the table of our products
Enter 2 to add a product
Enter 3 to caclulate the average cost of the items
Enter 4 to caclulate the total number of individual items
Enter 0 to exit the program
What would you like to do? 2

Please enter the product name: Chicken
Please enter the UPC: 25621
Please enter the price: 5.62
Please enter the number in stock: 30
Chicken added!

Enter 1 to display the table of our products
Enter 2 to add a product
Enter 3 to caclulate the average cost of the items
Enter 4 to caclulate the total number of individual items
Enter 0 to exit the program
What would you like to do? 0

I recommend you run your code with other various inputs to check to make sure your code is working correctly. Calculate the correct results by some other method (by hand, using Excel, etc), then confirm that your program gives the same results.

Data and Information

Guido’s Grocery is a small store so it doesn’t have a many products. Here is the initial product list and information about each product:

Product Name UPC Price (in dollars) Number in stock
Milk 95520 3.27 20
Eggs 55504 2.97 15
Bread 57971 2.78 20
Apples 19791 0.78 70
Cheese Bits 32510 2.99 25
Cheese Bytes 84159 23.92 10

(UPC stands for Universal Product Code. This is the number that shows up on barcodes. In real life they are usually 12 digits long.) (Please note that this data is fake, as is Guido’s Grocery.)

You will use a single nested list to store this information in your code. You will not receive full credit on this project if you do not use a nested list for this purpose. As shown above, each item should have its own row in the table. This means that each list within your one big list should represent an item and store its four properties: name, UPC, price, and number in stock. The product names should be stored as strings, the UPC and number in stock should be stored as integers, and the price should be stored as floating point values. Do not store the headers (“Product Name”, “UPC”, etc) in the 2D list - just the items and their data.

Requirements

Save your file as LastnamePA06.py where Lastname is your last name.

Your user interface should include the following:

  • Output - display a welcome message and instructions for various options
    • Tell the user to Enter 1 to display the table of our products
    • Tell the user to Enter 2 to add a product
    • Tell the user to Enter 3 to caclulate the average cost of the items
    • Tell the user to Enter 4 to caclulate the total number of individual items
    • Tell the user to Enter 0 to exit the program
  • Input - the program should ask for:
    • What the user wants to do (a number between 0 and 4)
  • Output - the program’s output will depend on what the user tells the program to do. Please see details for each function below.

Your program should have (at least) five functions:

  • A main function that controls the flow of the program. It should:
    • Create the initial 2D list that stores the initial product data
    • Display a welcome message
    • Continue to ask the user to input a number (0-4) until the user enters the number 0
      • If the user inputs 1, call the display_table function
      • If the user inputs 2, call the add_product function
      • If the user inputs 3, call the value-returning avg_price function and display a message using the value returned
      • If the user inputs 4, call the value-returning total_stock function and display a message using the value returned
    • Make sure you call main at the end of your code so the program runs!
  • A display_table function with one parameter variable: grocery_data which is a 2D list of product information
    • This function should display a table of the current products and their stored information. See the example outputs for details.
  • An add_product function with one parameter variable: grocery_data which is a 2D list of product information
    • This function should ask the user for details of the new product, then add this new product into grocery_data as the last product
      • The product names should be stored as strings, the UPC and number in stock should be stored as integers, and the price should be stored as floating point values.
    • After adding the item, display a message stating that the product has been added. Use the product’s name in this message.
  • A value-returning avg_price function with one parameter variable: grocery_data which is a 2D list of product information. It should return a floating point value.
    • Calculates and returns the average price of the items in the store.
  • A value-returning total_stock function with one parameter variable: grocery_data which is a 2D list of product information. It should return an integer.
    • Calculates and returns the total number of individual items in the store.

Other requirements

  • Include header comments at the top of the file that has at least your name, email, and a brief description of the program. The description should be 1 or 2 lines long describing the purpose of the program.
  • Include comments to label and describe each function.
  • Use comments elsewhere that you think would help guide the reader. You don’t want to overdo it though. Not every line of code needs a comment. Think in terms of blocks, or if a calculation is not clear.
    • Assume the reader has not read the homework assignment.
    • Assume the reader knows Python.
  • Include blank lines to separate the main sections of the code. This makes your code look more organized and easier to read.
  • Use descriptive variable names.
  • Your function names and variable parameters must be exactly as described above.

Code Organization

  • main function definition
    • Includes one function call to each of display_table, add_product, avg_price, and total_stock
    • See above for more information and details as to when these function calls should be made.
  • display_table function definition
    • Uses a nested loop to display a table of the current product information. See above for more information.
  • add_product function definition
    • See above for more information.
  • avg_price function definition
    • See above for more information.
  • total_stock function definition
    • See above for more information.
  • Call to main function

Hints

I recommend doing this in stages.

First create your main function. For now, just have it create the 2D list with the data from the table above. Follow the table examples from class if you get stuck.

Then work on the user interface. Initially, have it print out different messages if the user enters 1, 2, 3, or 4. (The messages don’t matter - they are just used for testing and can be replaced later). Test it with various inputs (0-4). Check to make sure that it continues to ask for inputs until the user inputs 0. Not sure how to get the while loop to run until the user enters 0 to exit? Refer to the How-To: Use Boolean Values as Flags page under Week 4 on Canvas.

Next, write your total_stock function. It is the easiest of the 4 non-main functions. Check to make sure it is working by calling it from user interface section in main under the case where the user types in 4.

Then write your avg_price function. Check to make sure it is working by calling it from user interface section in main under the case where the user types in 3.

Then I would recommend writing your display_table function. You have a few options for formatting the table. (If you get stuck on formatting the table, though, skip this and come back to it later.) One Python function you can use is ljust: str.ljust(number). This will format the string to be left justified and padded by number spaces.
I suggest using 15 for the padding. The first line of the table (the labels on the top), for example, can be generated with this line of code:

1
print("Product Name".ljust(15), "UPC".ljust(15), "Price".ljust(15), "Number in Stock".ljust(15), sep="")

You will need to use nested loops to display the data in the rest of the table. Work on getting everything displayed in the correct order first, then deal with the formatting. I do recommend that you have your display_table function working somewhat before working on add_product, so you can use display_table to check and see if your add_product function works.

Then write the add_product function. Use the display_table function to see if it is working properly. Make sure to call the other functions as well to make sure they still work after you add another item (or two!) to your table.

General Hints

Run your code every time you write a few lines to check for errors, rather than typing out the entire file at once then running it. This will make it easier to debug. If you get error messages, find the line number in the message. In a file in IDLE, the line number that the cursor is currently on is displayed on the bottom right of the window (look for Ln).

Reminders

Canvas will only let you submit a .py file, so make sure you use IDLE to write your code (and not something like Microsoft Word). When you first open IDLE, it shows the shell. Code you type in the shell will not be saved. Instead, when in IDLE click File then New File, and write your code here. You can then save it (click File then Save).