6.1.3 – Turn algorithms into Python

6.1.3 – Turn algorithms into Python

Translate informal pseudocode and an actual flowchart into Python. Preserve decisions, loop bounds and block boundaries, then check that the program follows the same routes.

Translate behaviour and order

An algorithm describes the steps that solve a problem. Pseudocode is an informal written description, not a second programming language with one compulsory syntax. A flowchart shows the same kind of control flow using shapes and arrows.

Conversion to Python must preserve the inputs, calculations, conditions, repeated actions and output order. Do not substitute a familiar program that produces one matching sample but follows different rules.

Pseudocode
INPUT age AS INTEGER
SET next_age TO age + 1
OUTPUT next_age
Python
age = int(input("Age: "))
next_age = age + 1
print(next_age)

Input is guaranteed valid integer text here. input() alone returns a string, so integer conversion implements the stated numeric input. SET becomes assignment. Python uses colons and indentation where pseudocode might use THEN, END IF or END WHILE.

Before writing code, mark what happens once, what happens only on a branch and what repeats. After writing it, follow one small example through both representations to check that the same state changes occur.

Keep the branch boundaries

Each borrowed book costs 2 credits. Borrowing five or more books reduces the final charge by 3 credits. Inputs are guaranteed non-negative integers.

Pseudocode
INPUT books AS INTEGER
SET charge TO books * 2
IF books >= 5 THEN
    SET charge TO charge - 3
END IF
OUTPUT charge
Python
books = int(input("Books: "))
charge = books * 2
if books >= 5:
    charge = charge - 3
print(charge)

For 6 books, charge changes from 12 to 9; for 3 books, it stays 6. The output lies after the decision, so both routes display a result. Indenting the print inside the if would silently remove output for fewer than five books.

Two-way decisions need an alternative branch. Preserve the exact boundary: “more than 4” excludes 4, whereas “at least 4” includes it. Equivalent Python conditions are acceptable when they select exactly the same stated inputs.

Follow an actual flowchart

A rounded terminator marks start or end. A parallelogram represents input/output. A diamond tests a condition; follow the labelled Yes or No arrow. Each route below finishes after exactly one output.

[DIAGRAM: asset_name: Parcel classification flowchart; asset_slug: parcel-flowchart-portrait; description: Portrait flowchart. Start, input parcels, test parcels == 0. Yes outputs Empty and ends. No tests parcels <= 5. Yes outputs Standard and ends. No outputs Bulk and ends. All input is a non-negative integer.]
Diagram

The input is a guaranteed non-negative integer. The first Yes route handles zero. Reaching the second decision establishes that parcels is not zero, so its Yes route covers 1 to 5. The remaining route covers values above 5. This complete description also states every diagram route in words.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account