Variables, input and output
Computer Science · Key Stage 3 · The School
Variables are labelled boxes
A variable stores a value under a name: name = "Aisha" puts the text "Aisha" in a box labelled name. score = 10 stores a number. print(name) outputs what the box holds. Change the box (score = score + 1) and the old value is replaced — the label stays, the contents update.
Talking to the user
answer = input("What is your name? ") shows the question, waits, and stores whatever is typed. Then print("Salam, " + answer) greets them by name. Input in, stored in a variable, transformed, output back — the same in→think→out shape as ever, now in real code.
Strings vs numbers
input() ALWAYS gives text (a string), even if the user types 12. "12" + "12" is "1212" — text glued together, not maths! To do arithmetic, convert: age = int(input("Age? ")), then age + 1 works. int() for whole numbers, str() to go back to text for printing.
Tracing a program that is wrong
Here is a program that runs without any error message and still gives a wrong answer: age = input("Age? ") then next_year = age + 1 then print(next_year). Typing 12 does not print 13 — Python stops with a type error, because you have asked it to add a number to text. Change the second line to next_year = int(age) + 1 and it works. When something behaves oddly, build a TRACE TABLE: one column per variable, one row per line of code, and write down what each holds after that line runs. For score = 5, then score = score + 3, then score = score * 2, the table reads 5, then 8, then 16 — and it is immediately obvious that the answer is not 5 + 3 × 2 = 11, because each line finishes completely before the next one starts. Tracing by hand finds bugs that staring at the code does not.