Programming turns a clearly described problem into instructions that produce a useful result. Variables, control flow, functions, and debugging are the main tools involved in the [programming concepts in pseudocode](/tech/computer-programming-basics-beginners-guide-for-starters). They make more sense when used together to solve a small problem than when memorized as separate terms.
This example builds a simple checkout calculation: add item prices, apply a discount when the subtotal reaches a threshold, and report the final total.
1. Translate the Problem Into Inputs, Rules, and an Expected Result
Before writing code, explain the program’s job in ordinary language. Computers follow instructions exactly, so vague directions cause trouble. A computer will not know that “a large order” means “an order of $50 or more” unless that rule is written down.
For the checkout example, the program needs the following:
Inputs
- A list of item prices
- A discount threshold
- A discount rate
Processing rules
- Add all item prices to find the subtotal.
- Apply a 10% discount when the subtotal is at least $50.
- Subtract the discount from the subtotal.
Edge cases
- What happens if there are no prices?
- Are negative prices allowed?
- Does a subtotal of exactly $50 qualify for the discount?
Expected result
- Produce and report the subtotal, discount amount, and final total.
For example, with prices of $12, $25, and $18:
- Subtotal: $55.00
- Discount: $5.50
- Final total: $49.50
The wording “at least $50” is important. A subtotal of exactly $50 qualifies for the discount. If that choice is not settled before coding begins, the later comparison can easily be wrong.
Real story
I once wrote a checkout script and felt very proud until I tested it with a cart total of exactly $50, which was the discount cutoff. The program cheerfully applied the discount twice and told me the customer owed negative three dollars, which felt bold even for software. I sat there staring at the terminal like it had personally robbed a grocery store.
Have a story of your own? Share it in the comments below.
2. Use Variables to Represent the Program’s Changing State
Variables give names to values a program needs to remember. In this checkout calculation, the program tracks the running subtotal, discount rate, discount amount, and final total.
Here is a small Python-style example:
subtotal = 55.00
discount_rate = 0.10
discount_amount = subtotal * discount_rate
final_total = subtotal - discount_amount
Each variable has three useful aspects:
- Its name, such as
subtotal - The value or object it currently references, such as
55.00 - The type of that value or object, such as a number, text value, or list
In Python, a variable name does not have a permanent type. The same name can later refer to an object of another type, although changing types without a good reason can make code more difficult to follow.
A variable’s value can also change while the program runs. A subtotal typically begins at zero and increases as the program processes each item.
subtotal = 0
subtotal = subtotal + 12
subtotal = subtotal + 25
subtotal = subtotal + 18
After these instructions run, subtotal contains 55. It is not a permanent equation; it is a named place for the current value.
Descriptive names prevent mistakes. Compare final_total with x, or discount_rate with dr. The shorter names save a few keystrokes, but they make the code harder to read later—even for the person who wrote it last Tuesday.
In real payment systems, prices are often stored as whole cents or represented with decimal types instead of ordinary floating-point numbers. This prevents small rounding surprises, such as a result displayed as 49.499999. For learning the program flow, simple decimal-looking numbers are sufficient.
3. Control Flow With Conditions and Repetition
Programs do not always execute every instruction in a straight line. They may repeat an operation for each item and then choose between two paths based on a rule.
Add Each Item With a Loop
Rather than writing a separate addition for every price, store the prices in a list and handle them one at a time.
prices = [12, 25, 18]
subtotal = 0
for price in prices:
subtotal = subtotal + price
The loop runs once for every value in prices. On each pass, it adds the current price to subtotal.
The values change as follows:
| Current price | Subtotal after adding it |
|---|---|
| $12 | $12 |
| $25 | $37 |
| $18 | $55 |
The loop reflects the original requirement: the program must handle multiple items, whether an order contains two items or twenty. Writing the same addition by hand would be tedious and would create plenty of opportunities for typos.
Apply the Discount With a Condition
After calculating the subtotal, the program checks the discount rule.
discount_threshold = 50
discount_rate = 0.10
if subtotal >= discount_threshold:
discount_amount = subtotal * discount_rate
else:
discount_amount = 0
final_total = subtotal - discount_amount
The if statement marks the decision point:
- If the subtotal is $50 or more, calculate the discount.
- Otherwise, set the discount to zero.
The >= operator means “greater than or equal to.” The equals part handles the boundary correctly:
subtotal = 50
This subtotal qualifies because the rule says “at least $50.” If the code used > instead, a customer spending exactly $50 would not receive the discount. It is a small character with a significant effect on the final bill.
4. Package Repeated Logic Into Functions With Clear Inputs and Outputs
After the basic calculation works, functions can divide the program into smaller tasks. A function accepts inputs, performs one focused operation, and returns an output.
Here, calculating the subtotal and applying the discount are separate jobs.
def calculate_subtotal(prices):
subtotal = 0
for price in prices:
subtotal = subtotal + price
return subtotal
The parameter prices is the function’s input. The variable subtotal is local to the function, so it exists only while that function runs. The return statement passes the completed subtotal back to the rest of the program.
A second function can handle the discount rule:
def apply_discount(subtotal, threshold, rate):
if subtotal >= threshold:
return subtotal * rate
return 0
The function can then be reused with different orders:
discount_a = apply_discount(55, 50, 0.10)
discount_b = apply_discount(42, 50, 0.10)
The first call returns 5.5; the second returns 0.
Functions work best when they represent a meaningful task and have clear inputs and outputs. A name such as apply_discount tells the reader what the function does. By contrast, a function that adds two numbers only once or relies on hidden variables elsewhere in the program can make simple code harder to understand.
5. Debug the Program by Reproducing, Isolating, and Verifying the Error
Debugging is not a matter of guessing until the program stops complaining. It involves comparing the program’s actual behavior with its intended behavior.
Start by identifying the general type of problem:
- A syntax error means the code is not written in a form the language can read, such as a missing punctuation mark or unmatched parenthesis.
- A runtime error occurs while the program is running, such as trying to use a value that does not exist or dividing by zero.
- A logic error occurs when the program runs but produces the wrong result.
Logic errors can be especially difficult because the code may look reasonable. Consider this incorrect discount function:
def apply_discount(subtotal, threshold, rate):
if subtotal <= threshold:
return subtotal * rate
return 0
The comparison is backward. The code discounts smaller orders and gives no discount to orders that qualify.
Work Through the Error Step by Step
-
Reproduce the problem with a small example.
Use a subtotal of $20, which is below the $50 threshold.
subtotal = 20 threshold = 50 rate = 0.10 discount = apply_discount(subtotal, threshold, rate) print(discount)The result is
2.0, but the expected result is0. -
State the rule in plain language.
The discount applies when the subtotal is at least $50. A subtotal of $20 does not qualify.
-
Inspect the values used in the decision.
Define and examine the values from the failing test, including the result of the condition.
subtotal = 20 threshold = 50 rate = 0.10 print(subtotal) print(threshold) print(rate) print(subtotal >= threshold)With a subtotal of
20and a threshold of50, the final expression should beFalse. -
Check the comparison operator against the rule.
The faulty code uses
<=, meaning “less than or equal to.” The intended rule requires>=, meaning “greater than or equal to.” -
Change one thing, then run the same test again.
if subtotal >= threshold:Once changed, the $20 order receives no discount.
-
Test the boundary and a normal qualifying case.
One successful example is not enough. Tests should include the point at which the rule changes.
Prices Expected discount Reason [20]$0 Below threshold [50]$5 Exactly at threshold [30, 25]$5.50 Above threshold []Error or defined zero-total result Empty order rule [20, -5]Error Negative prices are invalid
The empty-list case requires a product decision. A checkout system may reject an order with no items, or it may return a total of zero. Either choice is valid if it is explicit and tested.
Negative prices should generally be rejected unless the program intentionally supports returns or credits. Otherwise, an accidental minus sign can quietly create an incorrect total.
6. Assemble the Constructs Into a Small, Testable Program
The complete solution follows a straightforward sequence: receive the data, store it in variables, process the list, make a decision, and return the result. Functions separate the two main tasks, while loops and conditions handle the details that change.
def calculate_subtotal(prices):
if not prices:
raise ValueError("At least one price is required")
subtotal = 0
for price in prices:
if price < 0:
raise ValueError("Prices cannot be negative")
subtotal = subtotal + price
return subtotal
def apply_discount(subtotal, threshold, rate):
if subtotal >= threshold:
return subtotal * rate
return 0
def calculate_checkout(prices, threshold, rate):
subtotal = calculate_subtotal(prices)
discount = apply_discount(subtotal, threshold, rate)
final_total = subtotal - discount
return subtotal, discount, final_total
prices = [12, 25, 18]
threshold = 50
rate = 0.10
subtotal, discount, final_total = calculate_checkout(prices, threshold, rate)
print(f"Subtotal: ${subtotal:.2f}")
print(f"Discount: ${discount:.2f}")
print(f"Final total: ${final_total:.2f}")
For the example prices, the program reports:
Subtotal: $55.00
Discount: $5.50
Final total: $49.50
The program is small, but it shows the same pattern used in larger software:
- Define the inputs, rules, and expected result.
- Implement the smallest clear version of the logic.
- Run it with a known example.
- Inspect the output and important variable values.
- Debug differences between expected and actual behavior.
- Test normal, boundary, and invalid cases.
Most programs are not completed perfectly in a single pass. They are built in small pieces, checked against real examples, and refined. Variables hold the current state, control flow directs decisions, functions organize the work, and debugging keeps the program connected to the original problem.



