Quarry School

Programming in Python / First programs

First programs

Subject: Computing

All 5 lessons, in order.

The unit at a glance

  1. Run your first Python program

    A program is a list of instructions that a computer carries out in order.

  2. Tell text from numbers

    A locker door has two printed tiles on it, a 7 and then a 2, and the label reads seventy-two.

  3. Give a value a name

    Think of a coat check.

  4. Read a reply from the keyboard

    Imagine a school fair with a badge marked 04 and four spare badges on a tray.

  5. Use an error message to fix a program

    Imagine a note tells you to fetch a box labelled “Ticket”.

Rules (5)
  • Run your first Python program

    Lines you want to keep go in the editor window. A line you want to try once goes after >>> in the Shell. Save the file, choose Run and then Run Module, and read the result in the Shell below the newest RESTART line. For print("Lantern") the printed line is Lantern. The quote marks belong to your instruction, and the >>> belongs to the Shell, so neither is part of the printed line.

  • Tell text from numbers

    Use addition for two integers and joining for two strings. Check for quote marks before deciding which operation this example uses.

  • Give a value a name

    An assignment has a name to the left of = and a value or calculation to the right. Python works out the whole right-hand side first, then ties the left-hand name to the result. If that name already had a value, it now names the new one instead. No other name changes.

  • Read a reply from the keyboard

    Keep the reply as text for a badge code, where every character matters. For a count written in digits, convert the reply with int(reply) before adding to it.

  • Use an error message to fix a program

    Start at the reported line or the spot IDLE marks. The mark gives you an approximate place to look. The fix may be nearby. For the misspelled name, compare the name inside the parentheses with the one beside =. For the unclosed call, look for the missing ). Make one change and run the file again so you can see what that change did.

Recipes (5)
  • Run your first Python program
    1. Open IDLE from your Python 3.13 installation. IDLE is an optional part of Python. If it is missing, look for instructions from whoever provided your Python. Depending on its settings and how you start it, IDLE may open an editor window when it starts. If you do not see the Shell, choose Run, then Python Shell, to open it.
    2. In the Shell, after the >>> prompt, type print("Lantern") and press Return. The Shell shows Lantern.
    3. Choose File, then New File, to open an empty editor window.
    4. In the editor window, type print("Lantern") on line 1. Type the instruction only, without >>>.
    5. Choose File, then Save. A new file has no name yet, so IDLE asks you for one with Save As. Type lantern.py. While a window has unsaved changes, its title has a * at each end. The title bar also shows the Python version, so check that it says 3.13.
    6. Choose Run, then Run Module. The IDLE documentation names F5 as the key for it. If you forgot to save, IDLE either asks you to save or saves for you, depending on its settings.
    7. Look at the Shell. You see a line containing RESTART, then Lantern, then the prompt.
    8. In the editor, change Lantern to Cove so the line reads print("Cove"). Save, then run again. Cove appears below a newer RESTART line. Lantern stays higher up, because the Shell keeps earlier output.
  • Tell text from numbers
    1. Find the + and the value on each side of it. In this lesson both values always have the same type.
    2. Look at the edges of each value for a pair of matching quote marks.
    3. If both values have quotes, they are strings: write the characters of the left value, then the characters of the right value.
    4. If neither value has quotes, they are integers: add them as you would on paper.
    5. Write what print shows: the result with no quote marks, on a line of its own.
  • Give a value a name
    1. Draw a state table with a column for the line, a column for what the right-hand side works out to, one column for each name and a column for output.
    2. Take the next line of the program. If it is an assignment, cover the left side with your finger and work out the right side, using the values in the row above.
    3. Write that result in the column of the name on the left. Carry every other name's value down unchanged from the row above.
    4. If the line is a print, write what it displays in the output column: the values in the order given, with one space between them.
    5. Repeat until the last line. The output column is your prediction. Compare it with the real output if you have Python 3.13, or with the printed answer if you are working on paper.
  • Read a reply from the keyboard
    1. Type the Python source, the lines of code shown below, in your editor. Keep the blank line after reply = input() and save the file.
    2. Run the saved file with Python 3.13. The input() call has empty parentheses, so it waits for your reply without displaying a message asking for it. Type the two digits 04 where the program is running and press Enter.
    3. Compare the two lines the program prints with the expected output. The Shell can show your typed reply above those lines. If you’re working on paper, follow the code one line at a time and write down what each print() displays.
  • Use an error message to fix a program
    1. In IDLE for Python 3.13, use File, then New File to open an Editor window, where you type and save source. Save the file under the example’s name. In the Editor’s Run menu, choose Run Module. Read the program’s output in the Shell window. For these examples, type the source in the Editor and leave the Shell for reading.
    2. For the misspelled name, find the file name and line number in the Shell report. Open that file in the Editor if needed. Go to the indicated line, counting from 1 and including blank lines. Read the error class on the report’s last line. For the unclosed call, look for the spot IDLE marks in the Editor during the syntax check.
    3. Make one change in the Editor and save the file.
    4. Choose Run Module again and compare what appears in the Shell with your predicted output.
Traps (5)
  • Run your first Python program

    The usual mistake is writing the expected output as >>> Lantern, or as Lantern inside quote marks. The >>> is the Shell's prompt. The Shell shows it after the run to say it is ready for your next line, so it is not part of what your program printed. The quote marks belong to your instruction, and the printed line is Lantern alone. A related slip is copying >>> into the editor window. The file holds only your instruction. If nothing new appears, check two things. Look in the Shell, because the result never appears in the editor. Then look at the window title, because a * at each end means your latest change is not saved yet.

  • Tell text from numbers

    A natural mistake is to assume that anything made of digits will add. You see a 7 and a 2 in both 7 + 2 and "7" + "2", so it feels natural to expect 9 from both. The quote marks change the type: inside them each digit is a character tile, and + sets the tiles side by side to make 72. Before you predict a +, look at the edges of each value, not at the digits.

  • Give a value a name

    The usual mistake is to predict 5 5. It comes from reading spare = boxes as a lasting link, as if spare will always match boxes. That line ran once, at step 2, and tied spare to 4. Step 3 moved only the boxes tag, because boxes was the only name on the left.

    A second mistake is reading = as the equals sign from arithmetic. Read that way, boxes = boxes + 1 looks like nonsense, since no number equals itself plus one. In Python, = means "tie this name to this value". The comparison "is equal to" is written with two signs, ==, and a later unit teaches it.

    Avoid saying that spare = boxes "makes a copy". The Python tutorial states that a simple assignment never copies data. It puts a second name on the value that is already there. This lesson keeps to whole numbers on purpose.

  • Read a reply from the keyboard

    A reply can look like a number and still be text. Writing reply + 1 skips the conversion. Use int(reply) + 1 for the count. For this version, enter a whole number written in digits, such as 04 or 8. The word four is outside the form that int() can convert here.

  • Use an error message to fix a program

    Check the whole name when the error says NameError. If a call has an open parenthesis, find its closing partner before running the file. An unclosed call can stop the whole file during parsing.

Lesson 1 of 5

Run your first Python program

Subject: ComputingOpen this lesson
Leans on 0Comes back in 0

1 In plain words

A program is a list of instructions that a computer carries out in order. Python is a language for writing those instructions. Think of asking a friend to buy bread. You can say it out loud once, or you can write it on a note they can read again tomorrow. Python works both ways. You can try one instruction right now, or you can keep instructions in a file and run that file whenever you like.

You will use IDLE, Python's Integrated Development and Learning Environment, for both. IDLE has two main kinds of window: the Shell and the editor. The Shell is where you try one instruction at a time and where results appear. When it waits for you, it shows >>>, a mark called the prompt. An editor window is where you write instructions into a file and save them. IDLE treats a file whose name ends in .py, such as lantern.py, as Python code. The title bar is the strip across the top of a window, and in an editor it shows the file's name.

A few more words before you start. An instruction is also called a statement. To run a file means to tell Python to carry out every statement in it. In IDLE's menus a Python file is called a module, so Run Module means "run the file in this editor window". Output is whatever the program shows you when it runs.

Your first instruction is print("Lantern"). Read it aloud as "print the text Lantern". The word print names the instruction. The round brackets hold what it should print. The quote marks show where the text starts and stops. A piece of text in quote marks is called a string. Remember three things: the Shell is for trying one line now, the editor is for keeping lines in a file, and the output appears in the Shell either way. To look something up later, read the Run menu part of the IDLE documentation, which explains Run Module.

2 Picture it

Editorprint("Lantern")ShellLantern

In the editor, lantern.py contains print("Lantern"). Run Module sends the program to Python. In the Shell, Lantern appears between the restart notice and the next prompt.

From editor to Shell

3 Why

When you press Return in the Shell, it runs the one-line statement you typed. When you choose Run Module from an editor window, IDLE checks the file and, if it finds no error, restarts the Shell and then runs the file. The output is displayed in the Shell window, and it appears only when the program asks for it, for example with print. The restart shows up as a line containing the word RESTART.

The print instruction writes the text you give it and then ends the line. When you do not tell it where to write, it writes to a place called standard output. IDLE connects standard output to the Shell's display area, which is why the result appears there and not in the editor. When the run is complete, the Shell shows a prompt again so you can keep going.

4 The rule

Lines you want to keep go in the editor window.

A line you want to try once goes after >>> in the Shell. Save the file, choose Run and then Run Module, and read the result in the Shell below the newest RESTART line. For print("Lantern") the printed line is Lantern. The quote marks belong to your instruction, and the >>> belongs to the Shell, so neither is part of the printed line.

5 How

  1. Open IDLE from your Python 3.13 installation. IDLE is an optional part of Python. If it is missing, look for instructions from whoever provided your Python. Depending on its settings and how you start it, IDLE may open an editor window when it starts. If you do not see the Shell, choose Run, then Python Shell, to open it.
  2. In the Shell, after the >>> prompt, type print("Lantern") and press Return. The Shell shows Lantern.
  3. Choose File, then New File, to open an empty editor window.
  4. In the editor window, type print("Lantern") on line 1. Type the instruction only, without >>>.
  5. Choose File, then Save. A new file has no name yet, so IDLE asks you for one with Save As. Type lantern.py. While a window has unsaved changes, its title has a * at each end. The title bar also shows the Python version, so check that it says 3.13.
  6. Choose Run, then Run Module. The IDLE documentation names F5 as the key for it. If you forgot to save, IDLE either asks you to save or saves for you, depending on its settings.
  7. Look at the Shell. You see a line containing RESTART, then Lantern, then the prompt.
  8. In the editor, change Lantern to Cove so the line reads print("Cove"). Save, then run again. Cove appears below a newer RESTART line. Lantern stays higher up, because the Shell keeps earlier output.

6 Worked example

Run the saved file

Python code
print("Lantern")

Standard input

Empty

Standard output

Lantern

Save the line in lantern.py. Choose Run Module in the editor, then look in the Shell for the printed line.

Edit and run again

Python code
print("Cove")

Standard input

Empty

Standard output

Cove

Change the text in the editor, save and run the file again. The Shell shows Cove on its own line.

7 The trap

The usual mistake is writing the expected output as >>> Lantern, or as Lantern inside quote marks. The >>> is the Shell's prompt. The Shell shows it after the run to say it is ready for your next line, so it is not part of what your program printed. The quote marks belong to your instruction, and the printed line is Lantern alone. A related slip is copying >>> into the editor window. The file holds only your instruction. If nothing new appears, check two things. Look in the Shell, because the result never appears in the editor. Then look at the window title, because a * at each end means your latest change is not saved yet.

Quick checks

Quick check 1 of 2

A new file tide.py contains one line, print("Tide pool"). You save it and choose Run, then Run Module. Write the exact line the Shell displays as output.

Show the steps and the answer
Answer

Tide pool

Why: print writes the text and then ends the line. The quote marks belong to the instruction, and the >>> shown afterwards is the Shell prompt, not output.

Source: Python Software Foundation, Built-in Functions: print() (Python 3.13 documentation)

Quick check 2 of 2

You ran lantern.py with Run Module. In which window do you look for Lantern, and what kind of line comes just before it?

Show the steps and the answer
Answer

The Shell window. Just before the output is a line containing RESTART.

Why: Run Module restarts the Shell, then runs the file and shows its output in the Shell window, not in the editor.

Source: Python Software Foundation, IDLE: Python editor and shell (Python 3.13 documentation)

Sources

  1. IDLE: Python editor and shell (Python 3.13 documentation). Python Software Foundation. PSF License Version 2. Read 25 Nov 2026.
  2. Built-in Functions: print() (Python 3.13 documentation). Python Software Foundation. PSF License Version 2. Read 25 Nov 2026.

Lesson 2 of 5

Tell text from numbers

Subject: ComputingOpen this lesson
Leans on 0Comes back in 0

1 In plain words

A locker door has two printed tiles on it, a 7 and then a 2, and the label reads seventy-two. Nobody adds them. A basket holds 7 apples, you put in 2 more, and now it holds 9. The same two digits do two different jobs: on the locker they are characters you line up, and in the basket they are amounts you count.

Python keeps the same difference. Each piece of information Python works with is a value, and Python calls the kind of a value its type. A whole number written with digits alone, such as 7, is an integer, type int. Text is a string, type str.

You write a string by putting it between quote marks, and quote marks come in pairs: the first one opens the string and the matching one closes it. Double quotes and single quotes work the same, so "7" and '7' are the same one-character string. Digits inside quotes are text, not a number. "7" is a string and 7 is an integer.

Parentheses come in pairs too. In print(7 + 2), the ( right after print opens, the last ) closes, and what sits between them is what print shows. Type both halves of every pair: each opening quote needs its closing quote, and each ( needs its ).

An expression is a small piece of Python that works out to a value, such as 7 + 2. The + sign has two jobs. Between two integers it adds them. Between two strings it glues them together, end to end. So 7 + 2 works out to 9, and "7" + "2" works out to the two-character text 72.

This lesson uses Python 3.13, and you type it in one of two places. In the editor you type lines into a file, save it and run it, as in lesson 1. In the shell you type after the >>> prompt, and Python answers on the next line; a line with no >>> in front is Python's answer, not something you type. Without Python installed, follow the printed traces with pencil and paper.

In one line: quote marks turn digits into text, text joins and numbers add. Remember to look for quote marks before you predict what a + will do. To read more, look up "Text", section 3.1.2 of the Python 3.13 tutorial, listed in this lesson's sources.

2 Picture it

"7" + "2"727 + 29

For 7 + 2, seven counters plus two counters total nine. For "7" + "2", tile 7 followed by tile 2 reads 72.

Count objects or line up characters

3 Why

Python chooses what + does from the type of the values on each side, not from how they look on the screen. 7 and "7" show the same digit, but the first is an integer and the second is a string, because digits inside quotes are text. With two integers, + does arithmetic. With two strings, + glues the characters together, the left value first.

Python works out the expression inside the parentheses, then print turns the result into text and writes it on a line of its own. print leaves out the quote marks around a string. That is why print(7 + 2) shows 9 and print("7" + "2") shows 72, with no quote marks on either line.

4 The rule

Use addition for two integers and joining for two strings.

Check for quote marks before deciding which operation this example uses.

5 How

  1. Find the + and the value on each side of it. In this lesson both values always have the same type.
  2. Look at the edges of each value for a pair of matching quote marks.
  3. If both values have quotes, they are strings: write the characters of the left value, then the characters of the right value.
  4. If neither value has quotes, they are integers: add them as you would on paper.
  5. Write what print shows: the result with no quote marks, on a line of its own.

6 Worked example

Predict both lines

Python code
print(7 + 2)
print("7" + "2")

Standard input

Empty

Standard output

9
72

On paper, count seven objects and two more. Then put a tile marked 7 next to one marked 2. The total is 9 and the two tiles read 72.

7 The trap

A natural mistake is to assume that anything made of digits will add. You see a 7 and a 2 in both 7 + 2 and "7" + "2", so it feels natural to expect 9 from both. The quote marks change the type: inside them each digit is a character tile, and + sets the tiles side by side to make 72. Before you predict a +, look at the edges of each value, not at the digits.

Quick checks

Quick check 1 of 2

A locker label must show the digit 4 followed by the digit 1, two characters that read 41. Which line displays the label, print(4 + 1) or print("4" + "1")?

Show the steps and the answer
Answer

print("4" + "1"), which displays 41.

Why: Inside quotes, 4 and 1 are one-character strings, and + lines them up in order. Without quotes, print(4 + 1) adds the integers and displays 5.

Source: Python Software Foundation, 3. An Informal Introduction to Python (Python 3.13 tutorial)

Quick check 2 of 2

One shelf holds 3 jars and another shelf holds 6. Which line displays the total number of jars, print("3" + "6") or print(3 + 6)?

Show the steps and the answer
Answer

print(3 + 6), which displays 9.

Why: A total is an amount, so the values must be integers, and + adds integers. The quoted version joins two characters and displays 36, which reads like a label, not a count.

Source: Python Software Foundation, 3. An Informal Introduction to Python (Python 3.13 tutorial)

Sources

  1. 3. An Informal Introduction to Python (Python 3.13 tutorial). Python Software Foundation. PSF License Version 2 (documentation examples also Zero-Clause BSD). Read 25 Nov 2026.
  2. Built-in Functions: print (Python 3.13). Python Software Foundation. PSF License Version 2 (documentation examples also Zero-Clause BSD). Read 25 Nov 2026.

Lesson 3 of 5

Give a value a name

Subject: ComputingOpen this lesson
Leans on 0Comes back in 0

1 In plain words

Think of a coat check. The attendant ties a paper tag to your coat, and the tag tells everyone which coat is yours. Tie a second tag to the same coat and both tags lead to it. Move the first tag to a different coat and the second tag stays where it was, because nobody touched it.

Python names work like those tags, and the things they hang on are values. The line boxes = 4 reads aloud as "boxes now names 4", not "boxes equals 4". It is an instruction Python carries out, and nothing appears on the screen when it does.

A value is a piece of data. In this lesson every value is a whole number such as 4 or 5, which Python calls an int. A name, also called a variable, is a word you attach to a value so you can use that value again later.

In this lesson, every assignment is a line with one = sign: a name on the left, and a value or a calculation on the right. Everything after the = is the right-hand side. Giving a new value to a name that already has one is called reassigning, and the Python language reference calls it rebinding.

To trace a program means to run it by hand, one line at a time, in the order Python would. A state table is where you record the trace: one row for each instruction and columns for the right-side value, each name and output, showing its value after that line runs.

You met the shell and the editor in lesson 1. In the shell you type a line after the >>> prompt, and Python answers on the next line. In the editor you type a whole file, save it and run it. Every example here targets Python 3.13. You can follow along on your own computer, or with a pencil and the printed trace.

Remember one sentence: work out the right side first, then move the left-hand tag. To look it up, see section 3.1.1 "Numbers" of the Python 3.13 tutorial and section 7.2 "Assignment statements" of the Python 3.13 language reference.

2 Picture it

boxes4spare54boxesspare

The boxes and spare tags start together at 4. After boxes changes to 5, the spare tag stays at 4.

Two names can refer to the same value

3 Why

Python treats = as an order, not a question about whether two sides match. At an assignment, Python first works out the right-hand side to get one value. Only then does it attach the name on the left to that value.

That order is what makes boxes = boxes + 1 sensible. As an equation it could never be true. As an instruction it is clear: look up the value boxes names now, which is 4, add 1 to get 5, then move the boxes tag to 5. When a name already has a value, the assignment rebinds it, and the old value loses that tag.

Only the name on the left moves. spare = boxes asks for the value boxes names at that moment and ties spare to it. It does not tie spare to the word boxes, so a later line that moves boxes leaves spare where it was.

4 The rule

An assignment has a name to the left of = and a value or calculation to the right.

Python works out the whole right-hand side first, then ties the left-hand name to the result. If that name already had a value, it now names the new one instead. No other name changes.

5 How

  1. Draw a state table with a column for the line, a column for what the right-hand side works out to, one column for each name and a column for output.
  2. Take the next line of the program. If it is an assignment, cover the left side with your finger and work out the right side, using the values in the row above.
  3. Write that result in the column of the name on the left. Carry every other name's value down unchanged from the row above.
  4. If the line is a print, write what it displays in the output column: the values in the order given, with one space between them.
  5. Repeat until the last line. The output column is your prediction. Compare it with the real output if you have Python 3.13, or with the printed answer if you are working on paper.

6 Worked example

Trace boxes and spare

Python code
boxes = 4
spare = boxes
boxes = boxes + 1
print(spare, boxes)

Standard input

Empty

Standard output

4 5

Follow the rows below. For a check in the Shell, enter the three assignments and then enter each name on its own.

Trace

Follow boxes and spare line by line
InstructionRight sideboxesspareOutputExplanation
boxes = 444unset—The first line gives boxes the value 4.
spare = boxes444—Look up boxes first. Give spare that value.
boxes = boxes + 1554—Calculate 4 + 1 before giving boxes its new value.
print(spare, boxes)—544 5Print reads spare first and boxes second.

7 The trap

The usual mistake is to predict 5 5. It comes from reading spare = boxes as a lasting link, as if spare will always match boxes. That line ran once, at step 2, and tied spare to 4. Step 3 moved only the boxes tag, because boxes was the only name on the left.

A second mistake is reading = as the equals sign from arithmetic. Read that way, boxes = boxes + 1 looks like nonsense, since no number equals itself plus one. In Python, = means "tie this name to this value". The comparison "is equal to" is written with two signs, ==, and a later unit teaches it.

Avoid saying that spare = boxes "makes a copy". The Python tutorial states that a simple assignment never copies data. It puts a second name on the value that is already there. This lesson keeps to whole numbers on purpose.

Quick checks

Quick check 1 of 2

Trace this program with a state table before you run it. If you run it, type the four lines in the editor and save them as cups.py. The program reads no input. What does it display?

Python code
cups = 7
extra = cups
cups = cups + 3
print(extra, cups)
Show the steps and the answer
Answer

7 10

Why: Step 2 ties extra to 7, the value cups names at that moment. Step 3 works out 7 + 3 = 10 first, then moves only cups. extra stays on 7, and print shows extra, a space, then cups.

Source: Python Software Foundation, The Python Language Reference (3.13): 7.2 Assignment statements

Quick check 2 of 2

In this program the right-hand side of a later line uses a different name. If you run it, type the four lines in the editor. The program reads no input. What does it display?

Python code
seeds = 2
pots = seeds + 3
seeds = pots
print(seeds, pots)
Show the steps and the answer
Answer

5 5

Why: pots gets 2 + 3 = 5. In seeds = pots the right side is worked out first, and it is the value pots names now, 5. The seeds tag moves to 5, so both names end on 5.

Source: Python Software Foundation, The Python Language Reference (3.13): 7.2 Assignment statements

Sources

  1. The Python Tutorial (3.13): 3. An Informal Introduction to Python. Python Software Foundation. PSF License Version 2. Read 25 Nov 2026.
  2. The Python Language Reference (3.13): 7.2 Assignment statements. Python Software Foundation. PSF License Version 2. Read 25 Nov 2026.

Lesson 4 of 5

Read a reply from the keyboard

Subject: ComputingOpen this lesson
Leans on 0Comes back in 0

1 In plain words

Imagine a school fair with a badge marked 04 and four spare badges on a tray. If you copy the badge code onto a list, you write both digits. If someone brings one more spare badge, the tray holds five.

The line you give the program is its input. Read reply = input() as “read one line of text and save it under the name reply.” Remember that the reply is text. To use the digits as a number, look up int(), which turns digit text like 04 into a whole number.

2 Picture it

reply0404int(reply)4 + 1 = 5

Type 04 and press Enter. Keep both characters for a badge. To use the reply as a count, convert it to 4 and add one to get 5.

One reply, two uses

3 Why

Python calls a piece of text a string. When you type 04 and press Enter, input() returns a string, which means it gives text back to the program. The reply has two characters, 0 and 4. input() leaves out the line break at the end.

Python calls a whole number like 4 an integer. To convert is to change to a different form. In int(reply), the name inside the parentheses tells int() which text to convert. It reads 04 as the integer 4, and then + 1 adds one.

The program’s output is what it displays here. In this code, each print() displays one value on its own line. print(reply) displays the saved text, including the zero at the front. print(int(reply) + 1) displays the result of adding one to the count.

4 The rule

Keep the reply as text for a badge code, where every character matters.

For a count written in digits, convert the reply with int(reply) before adding to it.

5 How

  1. Type the Python source, the lines of code shown below, in your editor. Keep the blank line after reply = input() and save the file.
  2. Run the saved file with Python 3.13. The input() call has empty parentheses, so it waits for your reply without displaying a message asking for it. Type the two digits 04 where the program is running and press Enter.
  3. Compare the two lines the program prints with the expected output. The Shell can show your typed reply above those lines. If you’re working on paper, follow the code one line at a time and write down what each print() displays.

6 Worked example

One reply for a badge and a count

Python code
reply = input()

print(reply)
print(int(reply) + 1)

Prompt

Type 04 in the Shell and press Enter.

Standard input

04

Standard output

04
5

The first line of output keeps the two badge characters. To check the second line on paper, draw four strokes and add one.

Trace

How the reply becomes two output lines
InstructionExpression valueOutputExplanation
reply = input()text 04—The saved reply has two characters.
print(reply)text 0404Print writes both characters in their original order.
print(int(reply) + 1)number 55Integer conversion gives 4. Adding one gives 5.

7 The trap

A reply can look like a number and still be text. Writing reply + 1 skips the conversion. Use int(reply) + 1 for the count. For this version, enter a whole number written in digits, such as 04 or 8. The word four is outside the form that int() can convert here.

Quick checks

Quick check 1 of 2

A badge must show the typed code 04 exactly. What should the program print to keep both characters, reply or int(reply)?

Show the steps and the answer
Answer

Print reply. The badge shows 04.

Why: The saved text contains both characters, including the zero at the front.

Source: Python Software Foundation, Python 3.13 Built-in Functions (input)

Quick check 2 of 2

You run the same program and type 08. What appears on the first and second output lines?

Show the steps and the answer
Answer

The first line is 08. The second line is 9.

Why: The first line prints the saved text. For the second, int(reply) gives 8, then adding 1 gives 9.

Source: Python Software Foundation, Python 3.13 Built-in Functions (int)

Sources

  1. Python 3.13 Built-in Functions (input). Python Software Foundation. Python Software Foundation License Version 2. Read 25 Nov 2026.
  2. Python 3.13 Built-in Functions (int). Python Software Foundation. Python Software Foundation License Version 2. Read 25 Nov 2026.

Lesson 5 of 5

Use an error message to fix a program

Subject: ComputingOpen this lesson
Leans on 0Comes back in 0

1 In plain words

Imagine a note tells you to fetch a box labelled “Ticket”. The label on the box says “Tickets”. You can compare the two words, add the missing s to the note and try again.

The instructions you type for Python are called source. When you save your source in the Editor, you can give the file a name such as seats.py. If a message names seats.py and says line 2, read it as “look at the second line of seats.py”. The error class names the kind of problem. Remember where to look first, then read the class for a clue about what to change.

In seats = 3, = gives the name seats to the value 3. The marks ( and ) are parentheses. Writing print(seats) is a call. Python looks up the value of seats and passes it to print. The text the program prints is its output. A reply you type when a running program asks for one is supplied input.

2 Picture it

seats.pyseats = 3print(seat)NameErrorprint(seats)3

The file seats.py has print(seat) on line 2. The Shell reports NameError. Change seat to seats and run again to see 3.

Follow the error back to the line

3 Why

When line 1 gives 3 the name seats, line 2 has to use that same spelling to get the value by name. Python recognizes print(seat) as a call and hits an error when it looks for seat. The traceback is the part of the report that points to where the error happened. For this NameError, the class appears at the start of the last line of the report.

Syntax is the set of rules for how to write Python instructions, such as closing the parentheses in a print call. An unclosed call at the end of a file gives a SyntaxError before the file runs. When you choose Run Module, IDLE checks the file’s syntax before running it.

4 The rule

Start at the reported line or the spot IDLE marks.

The mark gives you an approximate place to look. The fix may be nearby. For the misspelled name, compare the name inside the parentheses with the one beside =. For the unclosed call, look for the missing ). Make one change and run the file again so you can see what that change did.

5 How

  1. In IDLE for Python 3.13, use File, then New File to open an Editor window, where you type and save source. Save the file under the example’s name. In the Editor’s Run menu, choose Run Module. Read the program’s output in the Shell window. For these examples, type the source in the Editor and leave the Shell for reading.
  2. For the misspelled name, find the file name and line number in the Shell report. Open that file in the Editor if needed. Go to the indicated line, counting from 1 and including blank lines. Read the error class on the report’s last line. For the unclosed call, look for the spot IDLE marks in the Editor during the syntax check.
  3. Make one change in the Editor and save the file.
  4. Choose Run Module again and compare what appears in the Shell with your predicted output.

6 Worked example

Find the missing letter

Python code
seats = 3
print(seat)

Standard input

Empty

Standard output

Empty

Exception NameError

Read the error class on the last line and use the file and line number to find the misspelled name.

Run the repaired file

Python code
seats = 3
print(seats)

Standard input

Empty

Standard output

3

Write seats next to 3 on paper. The second line looks up that name and prints the value.

Read a syntax error

Python code
print("Gate open")

print("Gate closed"

Standard input

Empty

Standard output

Empty

Exception SyntaxError

The file has an unclosed call. Python reports a syntax error during parsing, before either line prints.

Close the parentheses

Python code
print("Gate open")

print("Gate closed")

Standard input

Empty

Standard output

Gate open
Gate closed

Save the corrected file. Read the two lines in order to predict its output.

7 The trap

Check the whole name when the error says NameError. If a call has an open parenthesis, find its closing partner before running the file. An unclosed call can stop the whole file during parsing.

Quick checks

Quick check 1 of 2

Read this new file or type it in the Editor and save it as badges.py.

Python code
badges = 7
print(bagde)

Supplied input

None. What error class do you expect, what one change would you make, and what output do you expect in the Shell after running the repaired file?

Show the steps and the answer
Answer

Expect NameError. Change line 2 to print(badges) in the Editor.

Expected output after rerunning

7

Why: Python looks for bagde when it reaches line 2. Line 1 gave 7 the name badges.

Source: Python Software Foundation, 8. Errors and Exceptions

Quick check 2 of 2

Read this new file or type it in the Editor and save it as numbers.py.

Python code
print(5)
print(8

Supplied input

None. What error class do you expect, and what output will the file produce on that run?

Show the steps and the answer
Answer

Expect SyntaxError. The file produces no program output.

Why: The second call needs a closing ). The syntax check stops the file before either line runs.

Source: Python Software Foundation, IDLE — Python editor and shell

Sources

  1. 8. Errors and Exceptions. Python Software Foundation. Python Software Foundation License Version 2. Read 25 Nov 2026.
  2. IDLE — Python editor and shell. Python Software Foundation. Python Software Foundation License Version 2. Read 25 Nov 2026.