2.5 - While loops

While loops run a block of code until a condition is met. The while loop's syntax looks like this:

while condition:
    # Code

Let's continue this loop until the user types in "Hello"

text = "" # Set the text to nothing

while text != "Hello":
    text = input("Type Hello!")

print("Hello!")

We can visualize the code like this:

  1. Set the text variable to ""
  2. Is the text variable not equal to "Hello"?
    1. If so, set text to the user input
  3. If not, continue
  4. Print Hello!
There was meant to be an ad here. Support us by turning off your ad blocker or donating.