From the course: Python for Students

Unlock this course with a free trial

Join today to access over 25,500 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 else 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 costs an extra dollar. To do that, we're going to use an if statement and an else 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 toping == "cheese." We end it with a colon. Now we tell Python what we want to do if…

Contents