Reference no: EM132325438
In this Python coding where would examples of the following be?
A. Examples of four uses of list operations:
i. Creating lists
ii. Adding and removing data from a list
iii. Accessing values in a list
iv. Modifying values in a list
B. Examples of dictionary operations.
i. Creating dictionaries
ii. Adding and removing key-value pairs
iii. Accessing values using keys
iv. Modifying values
C. Examples of loop structures using comments 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
#Task: Create the empty data structure
grocery_history = []
#Variable used to check if the while loop condition is met
stop = 'go'
while stop.lower() != "q":
#Here we ask the user for input to help build the data dictonary for grocery items
item_name = input("Item name:n");
quantity = input("Quantity purchased:n");
cost = input("Price per item:n");
#Using the update function to create a dictionary entry which contains the name, number and price entered by the user.
grocery_item = {'name': item_name, 'number': int(quantity), 'price': float(cost)};
#Add the grocery_item to the grocery_history list using the append function
grocery_history.append(grocery_item);
#Now we need to ask the user if they are finished or would like to add more items.
stop = input("Would you like to enter another item?nType 'c' for continue or 'q' to quit:n");
# Define variable to hold grand total called 'grand_total'
grand_total = 0
#The for loop below is used to help perform the calculations to obtain the item total for each item in the grocery history list
for grocery_item in grocery_history:
#Calculate the total cost for the grocery_item.
print(" %d %s @ $%.2f ea $%.2f " %(grocery_item['number'], grocery_item['name'], grocery_item['price'], (grocery_item['price'] * grocery_item['number'])));
#Add the item_total to the grand_total
grand_total += (grocery_item['price'] * grocery_item['number']);
#Print the grand total
print("n Grand Total: $%.2f n" %(grand_total));