Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

''' 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 = {}

grocery_history = []

q = 0

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

stop = 'go'

while stop != q :

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

item_name=input('Item name: ')

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

quantity=input("Quantity purchased: ")

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

cost=input('Price per item: ')

#Create a dictionary entry which contains the name, number and price entered by the user.

grocery_item['name']=item_name

grocery_item['number']=int(quantity)

grocery_item['price']=float(cost)

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

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 grocery_item in grocery_history:

#Calculate the total cost for the grocery_item.

item_total = float(item['price']*item['number']);

#Add the item_total to the grand_total

grand_total = grand_total + item_total

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

#2 apple @ $1.49 ea $2.98

print(str(item['number'])+' '+item['name']+' @ $%.2f' %item['price']+' ea $%.2f' %item_total);

#Set the item_total equal to 0

item_total = 0

#Print the grand total

print 'Grand total = $'+str(grand_total)

With the code above I keep getting the error code "Item name: Quantity purchased: Price per item: Would you like to enter another item? Type 'c' for continue or 'q' to quit: Item name: Traceback (most recent call last): File "grocery_list.py", line 20, in item_name=input('Item name: ') EOFError: EOF when reading a line"

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To SQL

Authors: Frank Lusardi

1st Edition

0070390029, 978-0070390027

More Books

Students also viewed these Databases questions