You must first complete Adding Sleep before viewing this Lesson

Looping Your Code

Introduction

Our traffic light is looking good so far! However, no traffic light goes from red to green and stays green forever! Traffic lights usually repeat its sequence. In the programming world, we refer to this repeating of codes as “looping”. Let’s go ahead and loop our traffic light!

Components Needed

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

1

The while True block

This is the main block that we will be using as a loop for most of the early projects we have. As you can see, it is a bit different from the other blocks that we used as it has space inside it. This space is where we’ll be putting the code that we want to repeat.

2

Putting Our Code Inside the Loop

By putting our existing color change code inside the while True: loop, it will repeat the blocks inside it indefinitely

The Complete Code

Simply run the code!

Lesson tags: basic, color, neopixel
Back to: Beginner > Traffic Lights

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account