How to Create a Simple Grocery List Script in Python

In computer programming, a loop is a sequence of instructions that is continually repeated until a specified condition is reached. There are two types of loops in Python: the for loop and the while loop.
- For Loops – it allows us to repeatedly execute a block of code. This type of control is useful when we know how many iterations we want.
For example, I used the “For” loop in the calculation part because I wanted to repeat the calculation of all the items purchased.
- While Loops – these loops are used when we want to iterate until a certain condition is met. Note: do not create infinite loops. Set a certain condition to stop the loops.
For example, the “While” loop is used to repeat a block of code, so I used it to prompt a user to enter the item name, quantity purchased and price per item name. It executes the code block multiple times until a certain condition is met. For instance, the “While” loop will keep continuing until the user enters ‘q’ in this project. Once ‘q’ is entered, “While” loop stop.
I also want to mention a built-in data structure before creating today’s example script. Python has four built-in data structures: list, tuple, dictionary, and set.
- List – a collection of related objects. They might be zip codes, names, student IDs, product SKUs, etc.
- Tuple – like lists but are immutable.
- Dictionary – two columns of data. The first column contains a unique key, and the second column contains values associated with the keys. Each set of keys and values is referred to as a key/value pair.
- Set – An unordered set of related unique objects.
- This was my student project to learn basic python.
How to Create a Simple Grocery List Script in Python Click To TweetFor example, List and Dictionary are basically different data structures. I used Dictionary to see grocery items in this project because a dictionary is an implementation of a hash table. I used List to see grocery history in this project because a list can store data in a certain order and modified after it has been created.
Let’s Create a Simple Grocery List Script!
The project is broken into three sections:
- User input
- Loop through the grocery list
- Provide output to the console
Final Output:
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
Grocery List Script:
grocery_item = {}
grocery_history = []
stop = 'go'
while stop !='q':
item_name = input('Item name:\n')
quantity = input('Quantity purchased:\n')
cost = input('Price per item:\n')
grocery_item = {'name':item_name, 'number':int(quantity),
'price':float(cost)}
stop = input('Would you like to enter another item?\nType\'c\' for
continue or \'q\' to quit:\n')
grand_total = 0
for grocery_item in grocery_history:
item_total = grocery_item['number'] * grocery_item['price']
grand_total += item_total
print(str(grocery_item['number']) + ' ' + grocery_item['name'] + ' @ $' +
str(grocery_item['price']) + ' ea $' + str(item_total))
item_total = 0
print('Grand total: $' + str(grand_total))
Explanation line by line
Creating empty lists.
grocery_item = {}
grocery_history = []
Create a variable to check if the while loop condition is met.
stop = 'go'
This loop (while) will prompt the user to enter the item name, quantity purchased and price per item name. And, store the input value into a variable called grocery_item to create a dictionary. This loop will keep continuing until the user enters ‘q.’ Once ‘q’ is entered, while loop stops.
while stop !='q':
item_name = input('Item name:\n')
quantity = input('Quantity purchased:\n')
cost = input('Price per item:\n')
Create a dictionary. This is a key and value combination. In this line key is “name”, value is “item_name.”
grocery_item = {'name':item_name, 'number':int(quantity), 'price':float(cost)}
Use the append() method for modifying values.
grocery_history.append(grocery_item)
Prompt user to enter ‘c’ for continue, ‘q’ for quit.
stop = input('Would you like to enter another item?\nType\'c\' for continue or \'q\' to quit:\n')
Define a variable to hold a grand total.
grand_total = 0
This loop (for) will display each item you added. And also calculate each item and the total value. In this line, each grocery_history list element will be assigned to a variable named grocery_item.
for grocery_item in grocery_history:
Calculate the total cost for the grocery_item. (item quantity times price)
item_total = grocery_item['number'] * grocery_item['price']
Add the item_total to the grand_total.
grand_total += item_total
Output the information for the grocery item: item quantity, item name, item price per each and total price.
print(str(grocery_item['number']) + ' ' + grocery_item['name'] + ' @ $' + str(grocery_item['price']) + ' ea $' + str(item_total))
Set the item_total equal to 0.
item_total = 0
Output the grand total!
print('Grand total: $' + str(grand_total))
Wrapping Up
I hope you enjoy creating a simple grocery list as much as I enjoyed doing this! You can include these types of projects into your portfolio by creating your own site or simply uploaded to Dropbox and share it with your clients.