Python Break and Continue

PythonPythonBeginner
Practice Now

Introduction

In a distant ancient empire, there resided an Emperor who was not only powerful and wise but also deeply interested in the wonders of computation. The Emperor had heard of a remarkable language named Python that was capable of solving complex problems with elegant simplicity. He was determined to harness its power to manage the empire's vast resources more effectively.

An annual tournament was announced throughout the empire, where the smartest subjects would compete in a series of challenges to demonstrate their Python mastery. The ultimate goal for the participants was to impress the Emperor by showcasing their knowledge of Python's break and continue statements in loops, which represented the ability to navigate through the empire's vast treasury of information efficiently, allowing them to make swift and informed decisions.

The stage was set, and the challenge was clear: Use Python's break and continue statements to navigate through puzzles and data as deftly as the Emperor navigated through the affairs of his empire.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python/ControlFlowGroup -.-> python/break_continue("`Break and Continue`") subgraph Lab Skills python/break_continue -.-> lab-271523{{"`Python Break and Continue`"}} end

The Emperor's Challenge

In this step, you will be facing your very first challenge. The Emperor has provided a list of numbers representing different regions and their production output. He wishes to determine the first region that fails to meet a certain production quota. As his advisor, you must use a Python script to find this without checking all the regions. This is where the break statement will show its merit.

Write a Python script in the ~/project/emperor_python.py file to achieve the goal.

Here's a simple example code to get started:

quota = 500
production_list = [650, 624, 700, 480, 512, 575]

for production in production_list:
    if production < quota:
        print(f"Region under quota found with production: {production}")
        break

Run this script in your terminal with the command:

python3 ~/project/emperor_python.py

The information below should be displayed on your terminal:

Region under quota found with production: 480

Avoiding Unnecessary Battles

In this step, your skills will be tested further. The Emperor wants to know which of his allies are capable yet have minor setbacks, understanding the importance of continuity. You must use the continue statement to skip the regions that have exceeded a production surplus and only list those that have just fallen short of their surplus target but are still above the quota.

Modify your emperor_python.py to include the following:

surplus_target = 650
quota = 500
production_list = [700, 650, 480, 510, 690, 610, 725]

for production in production_list:
    if production >= surplus_target:
        continue
    elif production >= quota:
        print(f"Region close to surplus found with production: {production}")

Run the updated script in your terminal using the same command as before:

python3 ~/project/emperor_python.py

The information below should be displayed on your terminal:

Region close to surplus found with production: 510
Region close to surplus found with production: 610

Summary

In this lab, we embarked on an enlightening journey into the ancient empire's quest to master Python's control flow techniques. We learned how appropriate use of break and continue can make our code more efficient and tailored to the needs of the scenario. The design was aimed at relating the ancient tides of empire management with modern coding strategy, reminding us that smart decisions often lead to efficient outcomes. Applying these concepts in Python challenges helped us understand the practicality and necessity of control statements in programming. May the wisdom of the Emperor inspire you to continue honing your Python skills!

Other Python Tutorials you may like