☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

Thinking like a computer

الأسبوع 1 · الدرس ١ «التفكير الحاسوبي وحل المشكلات» (١): المهارات الأربع وخطوات حل المشكلة ومثال جمع رقمين · 3 دروس
3

Worked plan: adding two numbers

ICT Prep 3 Term 1, Unit 1, Lesson 1: pp. 6–7
GoalPlan the program that adds two numbers with the four skills and read its four lines of code.
New words
variable · a named box that stores a valueinput() · asks the user to type something and gives it back as textint() · turns text such as “4” into the whole number 4ValueError · the error Python shows when a value cannot be converted
Explanation

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.

Python
1num1 = int(input("First number: "))
2num2 = int(input("Second number: "))
3total = num1 + num2
4print(total)
Output
First number: 4
Second number: 9
13
Worked examples
SkillFor adding two numbers
Decompositionget, get, add, show
Patterninput → process → output
Abstractiontwo numbers and a sum; no colours
Algorithmread, read, add into total, show
Here are the four skills for the program as a table: decomposition gives get, get, add, show; the pattern is input, process, output; abstraction keeps only two numbers and a sum; the algorithm is the four-step list.
Python
1num1 = input("First number: ")
2num2 = input("Second number: ")
3total = num1 + num2
4print(total)
Output
First number: 4
Second number: 9
49
Nour forgets int() and writes the program like this. When the user types 4 and 9 the screen shows 49, because the plus sign joins two texts instead of adding two numbers.
Class activity

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.

Practice
1
Why does the program use int() after input()?
2
The user types 4 and 9 in the program of this lesson. What does the screen show at the end?
3
Why do we name the result total and not sum?
4
Which of these makes int() fail: the text 12, the word five, or the digit ٥?