Protect a budget using break Dry Run in PYTHON

Protect a budget using break is an interactive PYTHON dry run visualizer from the While programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Protect a budget using break Program Code

# Stop adding expenses before the budget is exceeded
expenses = [120, 180, 250, 90]
budget = 400
total = 0
i = 0

while i < len(expenses):
    expense = expenses[i]
    if total + expense > budget:
        print(f"Cannot add {expense}: budget would be exceeded")
        break
    total += expense
    print(f"Added {expense}, total = {total}")
    i += 1

print(f"Final spending = {total}")

View the complete While programs page.

Program Console Protect a budget using break Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.