Or Conditional

If I want the robot to drive while it’s not one color or another (ex: drive as long as it’s not red or green) which is the best way to code that? I’ve tried both ways in the attached screenshot. The first does not recognize the second color. The second does not recognize either color.
Thanks!
:slight_smile: Michelle

Great question!

The first set of code with the down_eye.detect() command has to keep in mind that this command only accepts a single parameter.

When using a color value such as BLUE or RED, you have to think of these as fixed value representations of the color. In C++, we’d think about these as an “enumeration” - An enumeration is a user-defined data type that consists of integral constants. In Python, they don’t have to be integral constants thanks to the type-less nature of the Python language.

So with all of that being said (BLUE or RED) is not valid syntax since you’re not actually passing a color value but instead you are passing a boolean expression (which is evaluating to either True or False) to a function expecting a color type - thus the unexpected behavior.

The second version is a tricky one! It’s helpful to think about the concept of each boolean expression (not green or not red) and visualize it with a truth table:

As you can see, because of the truth table, our expression will always evaluate as “True” because of how the OR operator works - it evaluates as “True” if one or both conditions are “True”. It only evaluates as “False” when both conditions are “False”, which never happens here.

So if we think about the logical statement we’re trying to accomplish - we want to drive forward if see Blue or None Color - if may see strange but you can accomplish this with the “and” operator:

With the “and” operator truth table with the two boolean conditions, you can see that the expression will only evaluate to “True” when the colors “Blue” and “None” are seen. This is because both individual conditions have to be “True” for the overall expression to evaluate as “True”.

It may seem counter-intuitive, but try the “and” operator instead and see if you get your expected results!

4 Likes

That makes perfect sense! Thank you so much for the truth table. I appreciate how detailed your explanations are. It will also help me to explain to the students better too! I made that change and it worked perfectly. I have learned so much over the past few days modifying the challenges to give the students more practice opportunities. Thank you for all your support.

:slight_smile: Michelle

3 Likes

@Tim_Friez that’s an amazing response, I second @Michelle_Sherry, the truth tables absolutely take the cake :slight_smile:

Glad to hear it @Michelle_Sherry! As you modify your resources, please feel free to share some of them here with us, I’d love to see what you’re using with your students!

1 Like

Here are the modified practice activities I made for unit 8. They were meant for students to read the code and trace the motion of the robot on the printed-out playground screenshots. I’m trying to get them to develop their mind’s eye.
Michelle_Sherry- Unit 8 Practice (1).pdf (358.4 KB)

:slight_smile: Michelle

2 Likes