From the course: Python for Students (2019)

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Adding an else

Adding an else

- [Instructor] When we make a decision, we're often deciding between taking one of at least two actions, based on the current conditions. For example, if it's raining out, I might want to put on my rain jacket, but if it's not raining, I will put on a sweater. In programming, we do that using an L statement. Let's take a look at this program. We have a list of pizza toppings, and then we're going to print out each of them. Let's go ahead and run it to see what happens. As you can see, it prints out the string, "We have the following toppings:" and then prints each item in the list of pizza toppings. What if we wanted to add the price of each of the toppings? Cheese is free, but pepperoni cost an extra dollar. To do that, we're going to use an if statement and an L statement. The first thing we'll do is we'll check if the element is cheese. We do that using an if statement, and we can just do if topping == and then the string, cheese. We end it with a colon.…

Contents