Question
''' The task is broken down into three sections. Section 1 - User Input Section 2 - loop through the grocery list Section 3 -
''' 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 = {} #defines empty dictionary grocery_history =[] #defines empty list
#Variable used to check if the while loop condition is met stop = 'go' while stop != 'q': #Creating a while loop as long as stop does not equal q
#Accept input of the name of the grocery item purchased. name = input("Item name: ") #Accept item name #Accept input of the quantity of the grocery item purchased. quantity = int(input("Quantity purchased: ")) #Accept quantity purchased #Accept input of the cost of the grocery item input (this is a per-item cost). cost = input("Price per item: ") cost = input("Price per item: ") #Accept price per item #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)} grocery_item = {'item_name': name, 'quantity': int(quantity), 'cost': float(cost)} #Control Structure - Create a loop #Loop will end when user enters 'q' #Create dictionary entry in loop containing name, number and price entered by user. Variable named grocery_item. #Add the grocery_item to the grocery_history list using the append function #grocery_history.append(grocery_item) grocery_history.append(grocery_item.copy()) #Accept input from the user asking if they have finished entering grocery items. stop = input("Would you like to enter another item? Type 'c' for continue or 'q' to quit :")
# Define variable to hold grand total called 'grand_total' grand_total = 0
#Define a 'for' loop. for item in grocery_history: #Calculate the total cost for the grocery_item. item_total = item['quantity'] * item['cost'] #Add the item_total to the grand_total grand_total += item_total # += is a compound operator #Output the information for the grocery item to match this example: #2 apple @ $1.49 ea $2.98 print("%d %s @ $%.2f ea $%.2f" %(item['quantity'], item['item_name'], item['cost'], item_total))
# Set the item_total equal to 0 item_total = 0
#Print the grand total print("Grand Total: $%.2f" %grand_total)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Test Your Code
Single Item, Check Input:
milk 1 2.99 q
Single Grocery Item Check
Check It!SHOW DIFF
LAST RUN on 1/31/2020, 9:49:46 AM
Check 1 failed
Output:
Item name: Quantity purchased: Price per item: Item name: Quantity purchased: Traceback (most recent call last): File "grocery_list.py", line 21, in
Expected:
Item name: Quantity purchased: Price per item: Would you like to enter another item? Type 'c' for continue or 'q' to quit: 1 milk @ $2.99 ea $2.99 Grand total: $2.99
Multiple Item Check Input:
milk 1 2.99 c eggs 2 3.99 c onions 4 0.79 q
Grocery List Feedback Available
Before you submit your code for grading, you may wish to get some feedback from our automated system. If so, just click on the Help Me! button below.
Help Me!
Multiple Grocery Check
Check It!SHOW DIFF
LAST RUN on 1/31/2020, 9:15:58 AM
Check 1 failed
Output:
Item name:
Quantity purchased: Price per item: Item name: Quantity purchased: Price per item: Item name: Quantity purchased: Price per item: Item name: Quantity purchased: Price per item: Item name: Traceback (most recent call last): File "grocery_list.py", line 19, in
Expected:
Item name: Quantity purchased: Price per item: Would you like to enter another item? Type 'c' for continue or 'q' to quit: Item name: Quantity purchased: Price per item: Would you like to enter another item? Type 'c' for continue or 'q' to quit: Item name: Quantity purchased: Price per item: Would you like to enter another item? Type 'c' for continue or 'q' to quit: 1 milk @ $2.99 ea $2.99 2 eggs @ $3.99 ea $7.98 4 onions @ $0.79 ea $3.16 Grand total: $14.13
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started