You must first complete Conditional Statements Part 1 before viewing this Lesson

Conditional Statements Part 2

Introduction

Now that we’ve learned how to display a different color based on user input, it’s now time to put our code in a loop so we can continuously change the light!

Components Needed

  • Little Python Brain
  • A tablet/phone/laptop
Steps

1

Looping the code

Take the inputif, and else blocks we have on our previous lesson and drag them inside a while True block.

2

The elif block

Another block we will be using is the elif condition block from the Operators category. We use this block when we have multiple conditions that we want to check for. 

For example, in this case we will be doing 3 different light settings: red, green, and blue. We’ve only done the code for the red light settings. We will use the elif condition blocks for the other two.

3

Where to place the elif blocks

Drag two elif condition blocks from the Operators category to our workspace. Place them after the if condition block but before the else block.

We would like a condition to check for “green” as well as for “blue”. Let’s set those conditions in two elif condition blocks. 

Afterwards, simply put the code inside to change the color to green and blue respectively.

4

Setting the conditions for the elif condition blocks

We would like a condition to check for “green” as well as for “blue”. Let’s set those conditions in two elif condition blocks. 

5

Setting the code to execute

Simply put the code inside to change the color to green and blue inside their corresponding elif condition blocks. 

We can run the code once everything is set!

The Complete Code

Back to: Beginner > Coder's Light Switch

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account