You are on page 1of 4

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

The format will be:


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

5. Add the grocery_item to the  grocery_history  list using the append function

6. 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.98

1 milk @ $3.99 ea $3.99

Grand 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'

Inside the for loop:


a. Calculate the total cost for the grocery_item.

The calculation is:

item_total = number * price

b. Add the  item_total  to the  grand_total

c. Output the information for the grocery item to match this example:

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
d. Set the  item_total  equal to  0

3. Finally, after printing all the items, print the grand total!

I. In Your Script (Annotated Text File):


A. Create examples of four uses of list operations in the script in your code. Be sure your examples
address each of the following:

i. Creating lists

ii. Adding and removing data from a list

iii. Accessing values in a list

iv. Modifying values in a list

B. Create examples of four uses of dictionary operations in your code. Be sure your examples address
each of the following:

i. Creating dictionaries

ii. Adding and removing key–value pairs

iii. Accessing values using keys iv. Modifying values

C. Create examples of three uses of loop structures in your code. Be sure your examples address each of
the following:

i. Item-based for loops

ii. Index-based (range) for loops

iii. While loops

You might also like