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:
- Set the
text
variable to""
- Is the
text
variable not equal to"Hello"
?- If so, set
text
to the user input
- If so, set
- If not, continue
- Print
Hello!
There was meant to be an ad here. Support us by turning off your ad blocker or donating.