Lists, Booleans, & User Input — How to Create a Grocery List Using Python

Candace Williams
thebit
Published in
5 min readDec 13, 2018

By the end of this tutorial, you’ll have created a super cool virtual grocery list that you can show off to your friends who still use … paper. The tools we need to accomplish this goal are lists, booleans, and input from the user.

Photo by Glenn Carstens-Peters on Unsplash

If you haven’t already, install Python from here. If you think you might need explicit instructions, check out this article.

Pre-Requisites: Nothing really, except for a bit of determination.

How Do We Start Off?

It’s always best to start off by initializing your variables. If you don’t initialize your variables (which means setting them to an initial value) before using them, Python will yell at you in red text, as it would break syntax rules. But before we initialize anything, we should understand the variables we’re going to use

Types of Variables

Just like in algebra, Python uses variables to store values. However, these variables can be named things other than x,y,z, etc. and their values don’t have to just be numbers. We’ll be utilizing two kinds of values in this tutorial

  • Booleans — Variables of the “boolean” type store a value of either True or False. For example: pinocchio_lied = True
  • Strings — Variables of the “String” type store text. The text is always encased between quotations. Ex. name = "Jenny".

Lists and Initialization

Get this, we can also assign multiple values to a single variable. Lists can be made up of one type ( ex. names = ["Sarah","John"] ), or multiple. This is where the core of our grocery list program lies.

Now, we can initialize our variables like so:

grocery_list = [] ##All lists follow the same syntax of [ , , ]
needs_items = True ##Do we need items? Yes

What’s Next?

Thinking Through It

We could jump right into the code here, but I’ve always found it easier to plan out what I’m going to do before I actually do it. This let’s us spend more time thinking about the logic of our program, rather spending a ton of time on Stack Overflow trying to figure out what went wrong.

So when building our grocery list, we should be thinking about the user and how they’re going to interact with what we’ve built. What goes on in your head when you’re listing out items on a piece of paper?

One, you start with a blank piece of paper (which is why we initialized our list earlier). You then ask yourself what you want to write, then you write what you have in mind. After writing down that specific item, you ask yourself if you want to add another item. If you do, then you’ll repeat the process. If not, you’ll stop thinking about adding items to the list, and you’ll hang it on the fridge for safe keeping.

Give yourself a pat on the back, you just learned how to use while loops, if statements, and user input without typing a single line of code.

Code, and Why It Works

Following the logic we just came up with, the code for our grocery list should look like this:

grocery_list = []
needs_items = True
while needs_items == True:

item_to_add = input("What item do you want to add? ")
grocery_list.append(item_to_add)
print("Your grocery list has the following items in it: ")
print("-----")
for item in grocery_list:
print("- " + item)
print("-----")

answer = input("Add another item? (y/n) ")
if answer == "n":
needs_items = False
print("Your final grocery list is: ")
print("-----")
for item in grocery_list:
print("- " + item)
print("-----")

If you’re seeing something like this for the first time and you can already feel a migraine coming on, just hold on a second — you’re most likely overthinking it. Let’s go through it together:

We already talked about the initialized values, so lets skip that part for now and zoom in on the “while” thing going on. while needs_items == True: just means that as long we still need items, we’re going to keep asking the user to add items to the list. We do all of the adding within the indented region below the while loop.

First, we ask the user what they want to add to the list using the input() method. We then assign whatever they give us to a variable called item_to_add. Once we have our value, we use the .append() method to add it to our grocery list, thus grocery_list.append(item_to_add). We could’ve stopped here, but we want to be able to see our progress.

To display the grocery list after an has been added, we loop through our list using something called a for loop.

for item in grocery_list:
print("- " + item)

We’re simply going through each item in the list and saying: for this item, print out the item. The “-” is being added to the item to a create a full string that encompasses both a bullet point and the name of the item.

The print("-----") statements you keep seeing around don’t impact the flow of the code. Rather, they make it easier for the user to understand what’s going on by separating each and every list we print out.

Once we finish adding a given item, we then ask the user if they want to add another item by insinuating an answer of “y” or “n,” that we can assign to the answer variable. We then use an if statement to determine what to do if the answer is “n,” no. If the answer is “n,” we change the state of our needs_items variable to False, because we know the user no longer wants to add items. As a result, when needs_items becomes false, the while loop will stop running and the user will stop being asked to add items.

The hard part is over! After the while loop, we’re simply printing out our final grocery list so that our user knows what their final product looks like.

When you run the code, you should be able to use your grocery list like so:

--

--