Quarry School

First programs — quick review

Run your first Python program

In plain words

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

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.

Step

  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.

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.

Tell text from numbers

In plain words

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

Rule

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

Step

  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.

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.

Give a value a name

In plain words

Think of a coat check.

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.

Step

  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.

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.

Read a reply from the keyboard

In plain words

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

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.

Step

  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.

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.

Use an error message to fix a program

In plain words

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

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.

Step

  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.

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.