Imagine building a game or a calculator from scratch using just three magical blocks. By linking text, numbers, and inputs together, you are about to create programs that actually talk back to you!
Words to own
- Trace
- Following code line-by-line in your head or on paper to predict exactly what it will do.
- Debugging
- The process of finding and fixing mistakes (bugs) in your code.
- Input
- Information that a user types into a program.
- String
- Computer science speak for a sequence of characters (text), wrapped in quotation marks.
- Integer
- A whole number (without decimal points) used for maths in coding.
- Type Conversion
- Changing data from one form to another, like turning the text '5' into the actual number 5.
The Power of Combining Blocks
Think of programming like playing with building blocks. On their own, a single plastic brick or a tiny plastic wheel isn't very exciting. But when you snap them together, you can build a spaceship, a castle, or a racing car.
In Python, our basic bricks are print(), variables, and input(). When we combine these three tools, we create interactive programs. These are programs that ask a user for information, process it, and output a personalized result. This is the exact pattern behind every app, game, and website you use every day.
- Step 1
1. Keyboard Input
The user types '3' on the keyboard. Python reads this as the text (string) '3'.
- Step 2
2. Conversion to Number
int() converts the text '3' into the actual integer 3 so we can do maths with it.
- Step 3
3. Calculation
The computer multiplies the integer 3 by the price ($12) to calculate the total (36).
- Step 4
4. Conversion to Text
str() converts the integer 36 back into the text string '36' so we can glue it to other words.
- Step 5
5. Screen Output
print() joins all the text pieces together and displays the final receipt on the screen.
The Greeting Machine (And How to Trace It)
Let's look at a simple program that greets a user. Before you run any code, it is vital to learn how to trace it. Tracing means reading the code line-by-line, pretending to be the computer, and predicting what will happen.
Tracing helps you understand exactly how variables change. For example, if a user types 'Ben' and '11', we can watch the computer store 'Ben' in the name variable, convert '11' into a number, and calculate next year's age. This stops mistakes before they even happen.
name = input("What is your name? ")
age = int(input("How old are you? "))
print("Hello, " + name + " - you are " + str(age) + " years old!")
next_year = age + 1
print("Next year you will be " + str(next_year))The Pizza Calculator (And the String Flip)
Now let's build something more practical: a calculator to find the cost of a pizza party. This program needs to do maths, which means we must convert the user's typed input into a number using int().
But here is the catch: when we want to print the final receipt, we have to do the reverse! Python cannot join text and numbers together directly using the + symbol. We must use str() to convert our total number back into text first. We call this type conversion.
name = input("What's your name? ")
num_pizzas = int(input("How many pizzas do you want? "))
price_each = 12
total = num_pizzas * price_each
print(name + ", your total for " + str(num_pizzas) + " pizza(s) is $" + str(total))Hunting Bugs (An Ancient Tradition)
When your code doesn't work, don't worry! Every programmer makes mistakes. The process of finding and fixing these mistakes is called debugging. It is like being a detective solving a mystery in your own code.
Did you know the word 'bug' for a computer error is actually real? In 1947, engineers working on an early giant computer called the Harvard Mark II found a real moth trapped inside a switch, stopping the machine from working. They taped the moth into their logbook and wrote 'First actual case of bug being found.' Ever since then, we have called fixing code 'debugging'!
Your turn — run real Python
The Movie Ticket Calculator
Write a program that calculates the total cost for movie tickets. The tickets cost $15 each. Ask the user for their name and how many tickets they want, then print a friendly receipt showing their total.
Bug hunt
Why does this code fail to calculate the correct cost of the lollies, and what happens when it tries to print?
name = input("Enter your name: ")
lollies = input("How many lollies? ")
cost = lollies * 2
print(name + " owes $" + cost)Myth-busting corner
MythComputers are smart enough to know that the typed text '5' is a number.
TruthComputers are actually quite simple! To them, the text '5' is just a symbol, like the letter 'A'. You must explicitly use int() to tell Python to treat it as a mathematical number.
MythYou can use the + sign to join text and numbers together without doing anything else.
TruthUsing + with two pieces of text joins them. Using + with two numbers adds them. But mixing text and numbers with + confuses Python, and it will stop running with a crash. You must use str() to convert numbers first.
Exam answer that scores full marks
Trace through this code line-by-line, explaining what each step does and predicting what is printed if the user types 'Aroha' and then '4': name = input('Name: ') sweets = int(input('How many sweets? ')) cost = sweets * 3 print(name + ' spent $' + str(cost))
Line 1 prompts the user and stores their input text, 'Aroha', inside the variable 'name'. Line 2 prompts the user for sweets, reads the typed string '4', converts it to the integer 4 using int(), and stores it in 'sweets'. Line 3 multiplies the integer 4 by 3 to get 12, storing this in 'cost'. Line 4 converts the integer 12 back into a string using str(cost), then joins it to 'Aroha' and ' spent $' using the + operator. The program outputs: 'Aroha spent $12'. Tracing code line-by-line is essential because it allows programmers to verify calculations and ensure type conversions occur in the correct sequence before execution.
Why it scores
- Correctly identifies how input is stored and converted to an integer on lines 1 and 2.
- Accurately performs the mathematical trace (4 * 3 = 12) and explains the conversion back to a string with str().
- Clearly states the exact predicted output string with correct spacing and punctuation.