Python Programming Fundamentals— Lists, Loops, and User Input

TheCyberBasics
Python in Plain English
5 min readMay 19, 2020

--

Last time, we created a simple function to convert Fahrenheit to Celsius. This was about as simple as we could go without simply printing “Hello World” to the screen. This time we’re going to take a look at lists, loops, and user input.

We’ll be working with another toy program in which a user will be prompted to pick their favorite food and the computer, in an attempt to please its new-found friend, will say that it is also its favorite food as well. Let’s dive right in and look at the code. Don’t worry if there are pieces you don’t understand, we’ll go over each part.

def fav_food():
foods = ["Pizza", "Cheeseburger", "Salad", "Chicken"]
for i,item in enumerate(foods):
print(i+1,item)
mine = input("Which of these is your favorite? : ")
print(foods[int(mine)-1 + " is my favorite too!")
def fav_food():

This part should be familiar from our previous program, we’re creating a function called fav_food. The empty () means we don’t provide any parameters to the function.

foods = ["Pizza", "Cheeseburger", "Salad", "Chicken"]

Here we are creating a variable named foods, however, instead of storing one value in our box, we’re storing multiple values. In fact, we’re storing a list of values. In Python, using the [] brackets indicate we are creating a list of items. Lists are comma-separated and can contain multiple different data types. Think of it as multiple boxes within your larger box. You could even store lists within your lists (crazy, I know).

In our case, each element of the list is a string of characters (called a string data type). Elements can be referenced by specifying their location within the list. This is a good time to mention that in programming languages sometimes you start counting at the number 0 and Python is one of those languages. For example, if you wanted to get “Salad” out of the list above you would reference:

foods[2]

To which you would see “Salad” printed to the screen.

for i,item in enumerate(foods):
print(i+1, item)

Here’s our loop. In this case, it’s a for loop and uses a special built-in function of Python called enumerate(). This will do two things, the first is it will iterate over the list (move from one item to the next) and keep count of the index of that item (stored in the variable i) and second it will keep track of the current item within the list (stored in the variable item).

The for portion of the line will repeat for every item within the list and will run whatever code is within the loop. In this case, we will be printing the current index (+1, since we start at 0) as well as the current item in the list. Let’s take a look at what this looks like:

Printing items in a list with enumerate()

As you can see, each item from the list is printed, along with its index in the list (+1).

Next, we need to get the user’s input to figure out what their favorite food is:

mine = input("Which of these is your favorite? : ")

We create a variable and instead of specifying what the value that will be stored is, we call another built-in function input which in our example takes one argument which is the message prompt printed to the screen. Any information provided by the user will be stored in the variable mine. For instance, if you typed the number 2 and pressed enter then mine would store the string value “2”.

Finally, we need to print message back to the user, proclaiming that their favorite food is also ours:

print(foods[int(mine)-1] + " is also my favorite!")

There’s some weird stuff happening here. Let’s look at the first piece:

foods[int(mine)-1]

Here we’re looking up a value in our list, and the location we’re looking at will be calculated by casting the user-supplied value as an integer (a number for our purposes) and subtracting 1. This is important because even though we don’t like starting at 0 for counting purposes, the list always starts at 0. Let’s take a look at some code running:

Printing User’s food choice

There are a few things being shown above. First, if we don’t cast our user’s input the interpreter considers it a string and causes a TypeError. Next, if we don’t subtract 1 from our user input we get the incorrect food item. Finally, by casting to an integer and subtracting 1 we get the actual food the user selected.

Once we have that information we can concatenate (or join) the food with the statement “ is also my favorite”. In Python, the plus sign + can be used for either addition (given numbers) or to join strings into one.

Let’s put everything together now, and see the final result:

I don’t think the computer is being honest

A curious reader could extend this example to allow the computer to select a random food, possibly a topic for the next tutorial ;).

If you enjoyed this content, please consider supporting over at Patreon (https://www.patreon.com/TheCyberBasics).

A note from the Plain English team

Did you know that we have four publications and a YouTube channel? You can find all of this from our homepage at plainenglish.io — show some love by giving our publications a follow and subscribing to our YouTube channel!

--

--