GCSE · Computer Science · Edexcel · Spec 1CP2
User input handling
Type 1, then 2, and ask Python to add them. Sometimes you get 3. Sometimes you get 12. Let's find out why.
Computer Science · Algorithms
Trace table — dry run the code
Same adding program, written two ways. Both times the user types 1, then 2. Lines 1-4 are version 1; lines 6-9 are version 2. Press Start and watch what num1 and num2 really hold. Quotation marks in the table mean a string.
Ready when you are — step through one line at a time.
Computer Science · Input and output
Going in or coming out?
Every one of these is either going in to a program or coming out of it. Pick an item, then pick its side.
Still to sort
Input (0)
Data sent to a program to be processed
Where the line is: Going in to the program: the user supplies it.
Output (0)
Information produced by a computer system as a result of processing data
Where the line is: Coming out of the program: the user receives it.
Predict, then check
In an f-string the curly brackets mark a variable, and its value goes into the text.
Program A: user = input(), then print(f"Hello {user}"). Program B: print("Hello Aisha"), with the name typed into the code itself. Each program is run twice. On its second run, Program A's user types Ben (on the first run they typed Aisha). What does each program print on the second run?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Whatever the user types, input() hands your program a string. Here's what that does to your sums.
What you need to know
- Input is data sent to a program to be processed; output is information produced as a result. A keyboard is a common way to give input.
- In Python, input() lets the user type something in, and print() displays information on the screen.
- user = input() stores whatever the user types in the variable user.
Have a goYour classmate Sam says: "print() is how the user types their name in." What has Sam got the wrong way round?
print() shows information on the screen; input() is what lets the user type.
Sam has swapped the two functions: print() sends information out to the screen, while input() takes what the user types in.
- An f-string puts a variable's value into text using curly brackets: print(f"Hello {user}") shows Hello Aisha if Aisha was typed.
Have a goThe user's name is in the variable user. Fill the gap so the program says Welcome, then the name: print(f"Welcome, ____")
{user}, so print(f"Welcome, {user}")
The variable goes inside curly brackets. Without them it isn't marked as a variable, so its value can't be inserted.
- Type Aisha into the code itself and the greeting never changes. The f-string's output follows what the user enters.
- input() gives you a string, even if the user types a number: a typed 15 is a string, not a number.
Have a goA user types 15 and the program runs age = input(). Is age holding the number 15, or something else?
Something else: the string "15".
It looks like a number, which is the tempting wrong answer, but input() returns the data as a string even when a number is typed.
- To use input as a number, cast it with int() or float(), like num1 = int(input()).
- Without casting, inputs of 1 and 2 get joined, so you output 12 instead of 3.
- Casting still can't predict the user: type four into an int() input and an error occurs, so the program crashes.
- Close print's brackets (or you get a syntax error), keep print lowercase (Print gives an error), and put curly brackets round f-string variables (or its name prints, not its value).
The big picture
Input is data sent to a program to be processed; output is information produced as a result. In Python, input() lets the user type, print() displays information, and an f-string puts a variable's value into text using curly brackets. input() returns a string even when the user types a number, so cast with int() or float() before doing sums, or 1 and 2 give 12. Casting can't predict the user: type a word such as four into a cast input and an error occurs, so the program crashes. Python's rules matter too: lowercase print, closed brackets, curly brackets round the f-string variable.
Key points
Worked example
Problem
A program asks for two numbers and prints their total. The user types 2.5, then 4. Write the program so that it prints 6.5, and say what it would print without any casting.
⚠ Watch out
Thinking that typing a number makes the program treat it as a number. input() always returns a string, so two typed numbers get joined by + until you cast them with int() or float().
Memory hook
Quotation marks mean string. Strings join, numbers add: cast first, then calculate.
Check yourself
A user types 4 and then 5 into two inputs that are not cast, and the program adds them with +. What is output, and which one change would make the program print 9?
Flashcards
(12)What is input?
What is output?
Which Python function lets the user type information in, and which displays information on the screen?
What does user = input() do?
What is an f-string?
Why does an f-string greeting change with the input, but typing Aisha into the code itself doesn't?
What type of data does input() give back, even when the user types 15?
How do you use typed input as a number?
Two uncast inputs, 1 and 2, are added with +. What is output?
The input is cast to an integer and the user types the word four. What happens?
Why can't casting stop every error?
What three rules apply to Python input and output, and what happens if you break each one?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
- Programming arithmetic operators
- Programming logical operators
- Programming relational operators
- Reading, writing, analysing and refining programs
- Single entry/exit code blocks
- String manipulation
- Structural components of programs
- Structured data types
- Validation – length check
- Validation – presence check
- Validation – range check
- Variables and constants in programs
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 12 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.