← Learn to Code

Proof Academy · Python · Journey 02

From text to numbers.

Build on variables and output. Supply inputs, choose types and test a calculator with zero and negative numbers. Work through the guide and calculator in two short sessions.

Guided study

Read it, trace it, test it

Follow the data through a program

Think in three stages. Input is data the program receives, processing transforms that data, and output communicates the result. Start by underlining which pieces of a brief belong in each stage. In a calculator, receiving 5 and 3 is input, adding their integer values is processing, and displaying 8 is output.

input() reads one line and returns a string. The characters might look numeric, but Python has received text. Text "005" keeps both leading zeros, while int("005") gives the number 5. Choose the type that suits the job. You would add two quantities as numbers, but usually keep a locker label or a telephone number as text because the exact characters matter. A decimal measurement might use float, which can represent a fractional value. A Boolean stores True or False; comparisons later in the course produce those values.

Work through a conversion

boxes = input()
per_box = input()
total = int(boxes) * int(per_box)
print(total)

Supply 04 on the first line and 6 on the second. After the first read, boxes is the string "04". After the second, per_box is "6". The two calls to int produce 4 and 6, and the product is 24. If you move int() outside an addition, int(boxes + per_box) would first make the string "046" and then turn it into 46. Work out the order of operations before adding brackets.

Read errors and test deliberately

  • If int("cat") fails, the input exists but cannot be converted. Python reports ValueError. int("3.5") also fails. The requirement here is a whole number rather than a measurement.
  • If the program asks for three lines and you supply only two, input() cannot read another line and reports EOFError. A blank line can be a supplied value. It still fails if you immediately try to turn it into an integer.
  • Adding an integer to a string gives TypeError. Check both operands. Use the variable names in the code and write down their types before fixing anything.

In the sandbox, set input lines first, then run. Try two normal cases, one involving zero and one with a negative number where the brief allows it. Calculate the expected answers on paper before checking the result. If the task requires exact output, an extra input("Enter a value: ") prompt can affect the comparison. The calculator exercises use plain input() for that reason.

Practise a Paper 2 habit

A provided file stores the cost in whole pounds and the number of tickets. Its output is 23 when the two inputs are 2 and 3, but the required result is 6. Explain the bug and repair it so the two input values are multiplied. Test 0 with 9 and 7 with 4. Then explain how you know the program used both inputs.

Check your guided practice

The faulty code joined two strings. Convert both values with int before multiplying. The three outputs should be 6, 0 and 28. An implementation that always prints 6 would pass only the first example. Also try changing one input while keeping the other the same, which helps identify whether both values are used in the calculation.

01 · Understand and predict

Input begins as text. Choose what it means.

Each input() takes the next supplied line and returns a string, even when it looks like a number. With inputs 5 and 3, adding the two strings joins them. To calculate, convert each value using int() before addition. A phone number should usually stay text so its leading zero is retained.

first = input()
second = input()
print(first + second)

02 · Run, modify and build

Build a two-number calculator.

Run the starter with 5 and 3, then convert both inputs to integers so it prints their sum. Use input() without prompts for this output check. Test the same program with 5 and 3 (8), 0 and 7 (7), and −2 and 9 (7). A solution must use the inputs, not print a fixed answer.

Supply answers before running. Each input() consumes one line, including a blank line. Asking for more lines produces EOFError. These are practice values; use invented data.

Passed with current code: 0/3.

Draft stays in this view only. Copy your work before leaving this page.

Output

Run your code to see its output here.

Ready when you are.

Python loads on demand from jsDelivr in an isolated workspace. Runs have a time and output limit, no account access, and no lasting files. Inputs are supplied before execution. Extra packages are outside this lesson. Progress here is practice feedback, not a saved exam result.

Need a hint?

Wrap each input() in int(). Converting after concatenation would turn 53 into the number 53, not the sum 8. Keep one print call for the final sum.

03 · Explain what changed

Explain the difference between text "5" and integer 5. Predict what happens if only one input is converted: adding an integer to a string raises TypeError. Repair it, then test zero and a negative number. A few passing tests are evidence, not proof for every possible input.

Next: selection and validation →Connect this to GCSE Computer Science →
Input and types in Python | Proof Academy