5 - Conditional Statements

If statements

Up until now, our programs have been running each line of code one after another. We have not given our programs any opportunities to make different decisions. For example, in an earlier example we asked the user what their favourite ice cream flavor was. Now what if we wanted to write a specific message depending on what their answer was. How would give our program the ability to make different decisions depending on the specific condition (or flavor!)? This is where ‘if statments’ become very useful!

 flavour = input(“What is your favourite ice cream flavour?”)if flavour == “chocolate”:     print(“I also like chocolate.”)

Try running this code. What do you notice? What do you think it happening? What happens if you enter ‘chocolate’?

So it seems that the program is somehow checking to see if the value of the flavour variable, which is assigned to whatever the user entered as their favourite ice cream, is equal to chocolate and if it is, it is printing our message. But how is it actually checking to see if the variable and the user input are equal? What is the double equals sign? Well, this is where we need to learn a little about relational operators. These are operators that will check the values on either side and decide if their relationship is True or False. They are super useful!

 Relational Operators == Checks to see if the two values are equivalent. 2 == 2 → True“Monkey” == “Banana” → False5 == 2 → False != Checks to see if the two values are not equivalent. 2 =! 5 → True“Monkey” != “Monkey” → False5 =! 5 → False >= Checks to see if the first value is greater than or equal to the second value. 2 >= 5 → False5 >= 5 → True10 >= 5 → True*You can compare strings but that will require a little extra explanation so we are going to skip that for now. <= Checks to see if the first value is less than or equal to the second value. 2 <= 5 → True5 <= 5 → True10 <= 5 → False*Same as above > Checks to see if the first value is greater than the second value. 2 > 5 → False5 > 5 → False10 > 5 → True*Same as above < Checks to see if the first value is less than the second value. 2 < 5 → True5 < 5 → False10 < 5 → False*Same as above

Ok, now that you know a little about relational operations we can go back to our ice cream flavour program. Yum!

So you likely noticed that if you enter anything other than ‘chocolate’ that nothing happens. That might be ok for some programs, but in this one we want to probably say something to the user. That is where we can extend our ‘if statement’ into an ‘if else statement’.

 flavour = input(“What is your favourite ice cream flavour?”)if flavour == “chocolate”:     print(“I also like chocolate.”)else:     print(“Good choice!”)

Now even if the user does not enter ‘chocolate’ they will still get a message letting them know that they made a good choice. It is important to realize that this message of “Good choice!” will be run by the program if the user enters anything that is not ‘chocolate’. This is what the ‘else’ does, it allows for “Good choice!” to be printed if anything ELSE other than ‘chocolate’ is chosen as the favourite flavour.

But what if you also want to tell the user a custom message if they like vanilla or strawberry ice cream. This is where we can use ‘elif’ (think of it is ‘else if’).

 flavour = input(“What is your favourite ice cream flavour?”)if flavour == “chocolate”:     print(“I also like chocolate.”)elif flavour == “vanilla”:     print(“Boring!”)elif flavour == “strawberry”:     print(“Yuck. I don’t like strawberry.”)else:     print(“Good choice!”)

Nested Conditionals

Great! You now have a basic understanding of conditionals (if statements). But it can often be useful to be able to nest multiple if statements together. For example, maybe your favourite flavour is peanut butter and chocolate ice cream so you might want to ask the user if they also like this flavour. But you only want to ask them this question if they chose chocolate. Using the template below see if you can update your code to ask the user if they like peanut butter and chocolate ice cream. If they say yes, tell them “Me too! I love it!” and if they say no then say “You should try it. So yummy!”.

 flavour = input("What is your favourite ice cream flavour?")if flavour == "chocolate":     print("I also like chocolate.")     # your if statement should be here (remember that      # indentation is important)elif flavour == "vanilla":     print("Boring!")elif flavour == "strawberry":     print("Yuck. I don't like strawberry.")else:     print("Good choice!")

 5.1 - You try!You are going to make a program that asks the user for any number. Your program will then tell them if their number is positive, negative, or zero. If it is zero, then you need to ask them if they want to know an interesting fact about zero. If they say yes, tell them an interesting fact. Don’t feel like searching for a fact about zero? Here are a couple:Zero is sometimes called nil or nought.In boolean logic zero represents false.Zero is neither positive or negative.