Get startedGet started for free

Basic while loop

Below you can find the example from the video where the error variable, initially equal to 50.0, is divided by 4 and printed out on every run:

error = 50.0
while error > 1 :
    error = error / 4
    print(error)

This example will come in handy, because it's time to build a while loop yourself! We're going to code a while loop that implements a very basic control system for a reverted pendulum. If there's an offset from standing perfectly straight, the while loop will incrementally fix this offset.

This exercise is part of the course

Intermediate Python for Data Science

View Course

Exercise instructions

  • Create the variable offset with an initial value of 8.
  • Code a while loop that keeps running as long as offset is not equal to 0. Inside the while loop:
    • Print out the sentence "correcting...".
    • Next, decrease the value of offset by 1. You can do this with offset = offset - 1.
    • Finally, print out offset so you can see how it changes.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Initialize offset


# Code the while loop
Edit and Run Code