...

/

Comparing and Choosing

Comparing and Choosing

Let's see how we can compare objects and make decisions in Python.

Coding is not just about writing a linear set of commands to execute. Sometimes, we need the code to behave in a certain way in one situation and another way in a different situation. To make decisions, however, we first need to assess the situation. We should be able to distinguish between the conditions to act accordingly. Did you notice at some point that Edward had some intelligence and could distinguish between things? If you did not, try interacting with the print_info() button in the widget below.

Notice that Edward has the ability to distinguish between whether the block it is in is empty, has a bucket filled with trash, or is occupied by a potted plant. But how is it getting this ability to distinguish between certain states of the block? It does that by comparing the state of the block it is currently in with the set of possible states. In the widget below, we have broken down this functionality to a lower level to show that the print_info() function does in the background. Give it a try!

Wow! So in the background, it was just checking these three conditions, whether they were True or False. It just checks which of the three variables is set to True, and it displays a ...