Need help with your Discussion

Get a timely done, PLAGIARISM-FREE paper
from our highly-qualified writers!

glass
pen
clip
papers
heaphones

Python Code

Question Description

Project 2: Grocery List

Introduction

Your task for this project is to create a very simple grocery list script. This script also emphasizes the importance of using lists, dictionaries and loop types within your script and how the use of those functions shapes your approach in creating a script.

The task is broken down into three sections:

  1. User Input
  2. Loop through the grocery list
  3. Provide output to the console

Data Structure

The structure of the data for this project is extremely important.

Each grocery item is a dictionary with the following key-value pairs

  • a name of the grocery item with the key identifier 'name'
  • the number of grocery items purchased with the key identifier 'number'
  • the cost of the grocery item with the key identifier 'price'

{'name': 'milk', 'number': int(1), 'price': float(2.99)}

grocery_history is a list of grocery item dictionary elements.

The following is an example of initializing a list with items:

grocery_history=[{'name': 'milk', 'number': int(1), 'price': float(2.99)},{'name': 'eggs', 'number': 2, 'price': 3.99},{'name': 'onions', 'number': 4, 'price': 0.79}]

Task: Create the empty data structure

  1. Create an empty dictionary with the variable name grocery_item
  2. Create an empty grocery_history list

Control Structure

The variable stop will be used with a while loop to check if the loop condition is true (and the loop will continue) or if the loop condition is false (and the loop will exit).

In this script, the loop will end when the user enters ‘q’

As the code in the while loop should run at least once, set the stop variable to anything other that ‘q’, e.g. 'go' or True

stop = 'go'

Write a loop that will be used to collect user input.

The While loop will use the stop variable to determine if the user has completed entering their grocery items.

The following steps will all be completed if the loop condition is met (i.e., the user has not entered 'q'):

  1. Accept input of the name of the grocery item purchased.
    Prompt: “Item name:n”
    Variable: item_name
  2. Accept input of the quantity of the grocery item purchased.
    Prompt: “Quantity purchased:n”
    Variable: quantity
  3. Accept input of the cost of the grocery item input (this is a per-item cost).
    Prompt: “Price per item:n”
    Variable: cost
  4. Create a dictionary entry in the loop which contains the name, number and price entered by the user. Assign the entry to a variable named grocery_item
  5. The format will be:

    {‘name’:item_name, ‘number’: int(quantity), ‘price’: float(cost)}

  6. Add the grocery_item to the grocery_history list using the append function
  7. Prompt the user for input asking if they have finished entering grocery items. Use the prompt:
    “Would you like to enter another item?nType ‘c’ for continue or ‘q’ to quit:n”

Looping Through the List

    In this section, you will loop through the grocery_history list. For each item, you will:

    • Print the number of that item purchased
    • Print the name of the item
    • Print the price paid for all of those items purchased
    • Add this price to the grand total for the entire list

    Your results will look similar to this:

    2 apple  @ $1.49 ea  $2.981 milk   @ $3.99 ea  $3.99Grand total: $6.97

    Print the List

    1. Define variable to hold grand total called grand_total
    2. Define a ‘for’ loop. This loop will examine each element in the grocery_history list.The loop will use the ‘for in’ convention. Each grocery_history list element will be assigned to a variable named ‘grocery_item’
    3. Inside the for loop:

      1. Calculate the total cost for the grocery_item.The calculation is:item_total = number * price
      2. Add the item_total to the grand_total
      3. Output the information for the grocery item to match this example:
      4. Output → 2 apple @ $1.49 ea $2.98
        Variable → number name price item_total

        Reminder

        Your can use the %.2f formatting expression to format the dollar amounts

      5. Set the item_total equal to 0
    4. Finally, after printing all the items, print the grand total!

    ”’

    The task is broken down into three sections.

    Section 1 – User Input

    Section 2 – loop through the grocery list

    Section 3 – provide output to the console

    ”’

    #Task: Create the empty data structure

    grocery_item = ?

    grocery_history = ?

    #Variable used to check if the while loop condition is met

    stop = ‘go’

    while ?????? :

    #Accept input of the name of the grocery item purchased.

    #Accept input of the quantity of the grocery item purchased.

    #Accept input of the cost of the grocery item input (this is a per-item cost).

    #Using the update function to create a dictionary entry which contains the name, number and price entered by the user.

    #Add the grocery_item to the grocery_history list using the append function

    #Accept input from the user asking if they have finished entering grocery items.

    # Define variable to hold grand total called ‘grand_total’

    #Define a ‘for’ loop.

    for ??????? in ?????????:

    #Calculate the total cost for the grocery_item.

    #Add the item_total to the grand_total

    #Output the information for the grocery item to match this example:

    #2 apple @ $1.49 ea $2.98

    #Set the item_total equal to 0

    #Print the grand total

Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."

Order Solution Now

Our Service Charter


1. Professional & Expert Writers: Eminence Papers only hires the best. Our writers are specially selected and recruited, after which they undergo further training to perfect their skills for specialization purposes. Moreover, our writers are holders of masters and Ph.D. degrees. They have impressive academic records, besides being native English speakers.

2. Top Quality Papers: Our customers are always guaranteed of papers that exceed their expectations. All our writers have +5 years of experience. This implies that all papers are written by individuals who are experts in their fields. In addition, the quality team reviews all the papers before sending them to the customers.

3. Plagiarism-Free Papers: All papers provided by Eminence Papers are written from scratch. Appropriate referencing and citation of key information are followed. Plagiarism checkers are used by the Quality assurance team and our editors just to double-check that there are no instances of plagiarism.

4. Timely Delivery: Time wasted is equivalent to a failed dedication and commitment. Eminence Papers are known for the timely delivery of any pending customer orders. Customers are well informed of the progress of their papers to ensure they keep track of what the writer is providing before the final draft is sent for grading.

5. Affordable Prices: Our prices are fairly structured to fit in all groups. Any customer willing to place their assignments with us can do so at very affordable prices. In addition, our customers enjoy regular discounts and bonuses.

6. 24/7 Customer Support: At Eminence Papers, we have put in place a team of experts who answer all customer inquiries promptly. The best part is the ever-availability of the team. Customers can make inquiries anytime.

We Can Write It for You! Enjoy 20% OFF on This Order. Use Code SAVE20

Stuck with your Assignment?

Enjoy 20% OFF Today
Use code SAVE20