Worked plan: adding two numbers
Let us plan “add two numbers typed by the user”. Decomposition: get the first number, get the second number, add, show. Pattern: input, process, output, like many programs. Abstraction: we ignore the colour of the window and the font; only the two numbers and their sum matter. Algorithm: read the first number, read the second number, add them into total, show total. In Python these are four lines. The function input() gives back text, so int() turns the text into a number; without int(), “4” and “9” would be joined into 49. We name the result total, not sum, because sum is already the name of a Python tool and our variable would hide it. If the user types a word such as five, int() cannot convert it and Python stops with a ValueError; Week 9 shows how to prevent this. Digits are fine even in Arabic-Indic form, as in ٥; the problem is words.
1num1 = int(input("First number: ")) 2num2 = int(input("Second number: ")) 3total = num1 + num2 4print(total)
First number: 4 Second number: 9 13
| Skill | For adding two numbers |
|---|---|
| Decomposition | get, get, add, show |
| Pattern | input → process → output |
| Abstraction | two numbers and a sum; no colours |
| Algorithm | read, read, add into total, show |
1num1 = input("First number: ") 2num2 = input("Second number: ") 3total = num1 + num2 4print(total)
First number: 4 Second number: 9 49
Plan, on paper, a program that multiplies two numbers typed by the user. Write the four skills in a table and then the steps. Say what the screen shows when the user types 6 and 7.