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.

1num1 = input()
2num2 = input()
3total = num1 + num2
4print(total)
5
6num1 = int(input())
7num2 = int(input())
8total = num1 + num2
9print(total)
num1
num2
total
Output
Press Start to run the first line.
·

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.

4 of 4 still to sort.

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?

Computer Science · Casting

Is casting a safety net?

Your program has the line num = int(input()). You've just learned that int() turns typed text into a number.

What happens if the user types the word four?
How sure are you?

Computer Science · Syntax

Find the slip

This program should ask for a name and greet the user. Python gives an error message instead of the greeting. Which line is to blame?

A student's program — which line goes wrong?

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

1Input goes in to a program; output comes out of it.
2input() collects what the user types, print() displays information, and an f-string's curly brackets insert a variable's value.
3input() always gives a string. Cast with int() or float() to use it as a number.
4Casting can't turn a word such as four into a number: an error occurs and the program crashes.
5Follow Python's rules: lowercase print, closed brackets, curly brackets round the f-string variable.

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?
Data that is sent to a program to be processed.
What is output?
Information produced by a computer system as a result of processing data.
Which Python function lets the user type information in, and which displays information on the screen?
input() lets the user type; print() displays information on the screen.
What does user = input() do?
It lets the user type, and stores what they type in the variable user.
What is an f-string?
A formatted string that inserts the value of a variable into text using curly brackets, like print(f"Hello {user}").
Why does an f-string greeting change with the input, but typing Aisha into the code itself doesn't?
The f-string inserts the variable's value, so the output follows what the user enters. A name typed into the code always prints the same greeting.
What type of data does input() give back, even when the user types 15?
A string, not a number.
How do you use typed input as a number?
Cast it with int() or float(), for example num1 = int(input()).
Two uncast inputs, 1 and 2, are added with +. What is output?
12. The typed values are joined instead of added; casting first gives 3.
The input is cast to an integer and the user types the word four. What happens?
An error occurs and the program crashes when it is run.
Why can't casting stop every error?
Because you cannot always predict what the user will enter.
What three rules apply to Python input and output, and what happens if you break each one?
print must be lowercase (Print gives an error message, because Python is case-sensitive); brackets opened after print must be closed (an unclosed bracket gives a syntax error message); an f-string needs curly brackets round the variable (without them there is no error, but the variable's name is printed instead of its value).

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 posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

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.