Python While Loops: Because Life Shouldn't Be a Never-Ending Loop

·

4 min read

In Python, a while loop is a control structure that allows you to repeatedly execute a block of code as long as the specified condition is true.

Basic Structure

  1. The while keyword is followed by a condition: This condition is a boolean expression that determines whether the loop should continue or not. As long as this condition remains true, the loop will keep executing.

  2. A colon (:): The colon signifies the start of the code block that will be executed repeatedly.

  3. Indented code block: All the statements inside the code block are executed repeatedly until the condition becomes false. Indentation is crucial in Python, as it defines the scope of the code inside the loop.

Example 1

Type the below code to your editor.

  1. The count int = 0: This line initializes a variable named count and sets its initial value to 0. The: int part is a type hint, indicating that count is expected to hold an integer value.
  1. while count < 5:: This line starts a while loop. The loop will continue to execute as long as the condition count < 5 is True. In other words, it will keep running until thecount is no longer less than 5.

  2. print(f"Count is {count}"): Inside the loop, there's a print statement. It prints a message that includes the current value of the count variable. The f"Count is {count}" is an f-string, which allows you to embed the value of count within the string.

  3. count += 1: This line increments the count variable by 1 in each iteration of the loop. This step is crucial because it ensures that the condition count < 5 eventually becomes False after the count reaches 5, terminating the loop as shown below.

    Example 2

In this instance, we will employ the input function. Please enter the following code:

  1. user_input = input("Enter 'quit' to exit: "): This line prompts the user for input by displaying the message "Enter 'quit' to exit: " and stores the user's input as a string in the variable user_input.

  2. while user_input != 'quit':: This line starts a while loop that continues as long as the value of user_input is not equal to the string 'quit'. In other words, it keeps running until the user types 'quit' to exit the loop.

  3. print(f"You entered: {user_input}"): Inside the loop, this line prints a message that includes what the user entered using an f-string. For example, if the user enters "hello," it will print "You entered: hello."

  4. user_input = input("Enter 'quit' to exit: "): After printing the message and displaying the prompt again, this line takes another user input and stores it in the user_input variable. This new value is checked in the next iteration of the loop.

  5. Here's how the program works:

    • It starts by asking the user for input and stores it in user_input.

    • It enters the while loop and checks if user_input is not equal to 'quit'.

    • If the user enters anything other than 'quit', it prints the input and asks for input again.

    • This process repeats until the user enters 'quit', at which point the condition user_input != 'quit' becomes False, and the loop exits.

In simple terms, this program keeps asking the user for input until the user types 'quit' to exit the program. It also informs the user of what they entered before asking for the next input as shown below.

Example 3

In this code example, we will simulate a hypothetical space journey using a while loop to count backward. While this simulation doesn't involve actual space travel, it offers an engaging way to practice programming with a while loop by counting in reverse.

  1. It takes user input to determine a starting number for a countdown.

  2. The function then enters a while loop that continues as long as the value of n (the starting number) is greater than 0.

  3. Inside the loop, it prints the current value of n and then decrements n by 1 in each iteration.

  4. Once the value of n becomes 0 (or less), the loop exits.

  5. After the loop, it prints "Blast off!" to indicate the end of the countdown.

Explanation

  1. The user enters 5 as the starting number.

  2. The while loop checks if n is greater than 0, which is true (5 > 0).

  3. It prints the current value of n, which is 5.

  4. It decrements n by 1, so now n is 4.

  5. The loop continues, printing 4 and then decrementing n to 3, and so on.

  6. This process repeats until n becomes 0.

  7. When n reaches 0, the loop exits, and "Blast off!" is printed to signify the end of the countdown as shown below.

A lot of the key concepts related to while loops have been covered in this article. In our next installment, we will explore the topic of for loops. Thank you for your attention, and stay tuned for the upcoming article.