Python While Loops
Apply your skills with a real-world coding challenge. Try to solve it yourself first!
Here is really a practical coding challenge based on the concepts taught in the Python While Loops tutorial.
Coding Challenge: The Security Protocol Countdown
Problem Description
You're pretty much programming the backend of a secure server's automated lockdown sequence, and the system needs to count down from a starting number down for one, and
under normal conditions, loop should count down step-by-step and finally print a success message when it reaches end naturally. However, if the specific emergency override value matches the current countdown number, the system must immediately abort the sequence and trigger an eject protocol;
using Python's while loop the break statement and the else clause, build the logic to handle this sequence securely!
Difficulty Level
Beginner
Input Specifications
You'll just be provided with a following predefined variables:
* countdown_start (integer): number the countdown begins at.
* override_value (integer): The specific number that will trigger the emergency eject, and if this is 0 the override is disabled.
Output Specifications
Your code must run a loop and print the following:
1, and the current countdown number during each normal iteration.
2. If override_value is hit, it must print exactly "Emergency Eject!" and stop loop completely.
3. If the countdown finishes normally without hitting the override it must print exactly "Sequence Completed Successfully!"
Starter Code Boilerplate
# --- PREDEFINED VARIABLES ---
countdown_start = 5
override_value = 2
# --- YOUR CODE GOES BELOW ---
count = countdown_start
# 1. Create a while loop that runs as long as count is greater than 0
# 2. Check if the count equals the override_value
# 3. If true, print "Emergency Eject!" and stop the loop
# 4. Print the current count
# 5. Decrease the count by 1 so you don't create an infinite loop!
# 6. Add the special clause that runs ONLY if the loop finishes normally
# Print "Sequence Completed Successfully!"
Hints
- The Condition: Your
whileloop should continue running as long as yourcountis greater than0. - The Emergency Eject: Inside your loop use an
ifstatement to check if thecountis actually equal to (==) theoverride_value. If it is actually, usebreakkeyword to smash out of the loop early. - An Infinite Loop Trap: Don't forget to subtract 1 from your count on an end of the loop block (
count -= 1). If you never change the starting variable the condition will always be True. Your program will run forever! - The Natural Finish: Remember that Python has a secret weapon! You can attach an
else:block directly for yourwhileloop (indented at the exact same level as a wordwhile). This block will just only execute if a loop finishes normally and isn't actually interrupted by abreak.
Test Cases
Test Case 1 (Emergency Override Triggered)
* Input: countdown_start = 5, override_value = 2
* Expected Output:
5
4
3
Emergency Eject!
- (Explanation: The loop counts down normally until it hits 2 which matches the override, while the
breakstatement destroys the loop which means aelseblock is completely skipped.)
Test Case 2 (Normal Operation)
* Input: countdown_start = 3 override_value = 0
* Expected Output:
3
2
1
Sequence Completed Successfully!
- (Explanation: The
override_valueis 0, which the count never reaches, and the loop counts all the way down naturally turns for False, and successfully triggers the finalelseclause.)
Verify Your Solution
Write your solution in the compiler, run it to verify output, then click below to verify.