Python While Loops

PythonPythonBeginner
Practice Now

Introduction

Welcome to the enchanted world of Magic Seas, where our protagonist, the wise Mariner Mage, is on a quest to discover the secrets of ancient marine spells. Amidst the swirling waters and mystical creatures, Mariner Mage needs to navigate through islands, where each holds a piece of the arcane puzzle. But the seas are treacherous, and the path ahead is unclear. To successfully accomplish their mission, the Mariner Mage must be well-versed in the art of Python magic, particularly the spell of 'While Loops'.

Our goal is to help the Mariner Mage complete this epic journey by mastering the power of while loops, ensuring that not a single island is left unexplored, and all secrets are unveiled. Are you ready to hoist the sails and set forth on this programming voyage?


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python/ControlFlowGroup -.-> python/while_loops("`While Loops`") subgraph Lab Skills python/while_loops -.-> lab-271607{{"`Python While Loops`"}} end

Setting Sail

In this step, you will help the Mariner Mage to set the sails for their journey. To start, you will create a spell (script) that checks the wind direction every day using a while loop until the perfect wind direction is found. We will simulate this by generating a random wind direction and testing it in our loop.

Edit the /home/labex/project/wind_checker.py file using your favorite editor, and insert the following code:

import random

## Define the wind directions
possible_directions = ["north", "south", "east", "west"]
## Set perfect direction to east
perfect_direction = "east"

## Start the while loop
while True:
    ## Simulate checking the wind direction
    current_direction = random.choice(possible_directions)
    print(f"The wind blows from the {current_direction}.")

    ## Check if the wind direction is perfect
    if current_direction == perfect_direction:
        print("Ahoy! Perfect wind for setting sails!")
        break
    else:
        print("Not favorable for sailing. Let's check again tomorrow.")

Run the script:

python wind_checker.py

This script will loop indefinitely until the current_direction matches the perfect_direction, which is set to "east". Every time the loop runs, it simulates waiting for a new day and checking the wind direction again.

The information below should be displayed on your terminal:

The wind blows from the north.
Not favorable for sailing. Let's check again tomorrow.
The wind blows from the west.
Not favorable for sailing. Let's check again tomorrow.
The wind blows from the east.
Ahoy! Perfect wind for setting sails!

Gathering Provisions

In this step, the Mariner Mage needs to gather enough provisions for the journey. We will simulate this by creating a provisions.py script that uses a while loop to count the number of provisions collected until the desired amount is reached.

Open /home/labex/project/provisions.py and add the following code:

## Number of provisions needed
needed_provisions = 10
collected_provisions = 0

## Start the while loop
while collected_provisions < needed_provisions:
    ## Gather provisions
    collected_provisions += 1
    print(f"Collected provisions: {collected_provisions}")

print("All necessary provisions gathered! Ready to set sail.")

Run the script:

python provisions.py

This code will use a while loop to gather provisions until collected_provisions equals or exceeds needed_provisions.

The information below should be displayed on your terminal:

Collected provisions: 1
Collected provisions: 2
Collected provisions: 3
Collected provisions: 4
Collected provisions: 5
Collected provisions: 6
Collected provisions: 7
Collected provisions: 8
Collected provisions: 9
Collected provisions: 10
All necessary provisions gathered! Ready to set sail.

Summary

In this lab, our journey followed the tale of the Mariner Mage setting out for a magical voyage. Through crafting Python while loops, you supported the Mage in waiting for the right wind and gathering sufficient provisions. This narrative approach aimed to instill a sense of adventure while reinforcing the fundamentals of loops, engaging creativity, and connecting code to a vivid storyline.

By envisioning programming concepts as magical elements within an immersive narrative, we've not only learned to control the flow of our code but also inspired our minds to explore endless possibilities. The Mariner Mage's success is a reflection of your mastery over while loops, and this lab is but the beginning of your Pythonic voyage across the vast seas of programming. Sail forth!

Other Python Tutorials you may like