← Teacher guides

Teacher guide: Console bridge: text in, text out

input(), print() and a text menu: the same logic as the turtle courses, as text in and text out. Every Python extension starts here.

Open the student course

Each lesson works two ways. Students can work through the online tutorial at their own pace (it checks their answers and saves their progress in their browser), or you can teach it to the whole class from the slides, then send students into the tutorial for the Modify and Make steps.

Every lesson guide has plain-language notes, the answers to every check, a solution for every task, and slides and a worksheet for Waypoints Prepare. These guide pages are not linked from student pages, but they are not secret either: keep printed Teacher editions for answers you do not want students to find.

Whole-course cheat sheet: Open to print (every command, grouped by unit: a handout for the final challenge)

Text in, text out

Show text, ask a question, store the answer and join numbers onto text.

Text in, text out in 5 minutes (read before teaching this unit)

The idea in plain words

A console program works with text instead of pictures. It shows a line, waits for someone to type an answer, and replies. Three pieces do almost all of it:

  • print("...") shows a line of text. Text goes in quotes, and the quotes are not shown.
  • + joins pieces of text into one: "Hello, " + name. It adds nothing, so a space only appears where it is inside the quotes.
  • input("question") shows the question, waits, and gives back what was typed. Store it with name = input(...).

A number cannot be joined onto text: "Score: " + 5 is an error. str(5) makes a text copy that can be joined.

Why programmers use them

  • Every app shows and asks. Forms, chat windows and ticket machines are the same idea with a nicer screen.
  • Messages are built from pieces. A greeting, a name and a count are joined into the line a person reads.
  • It is the bridge. The turtle courses' ideas (variables, decisions, loops, functions) carry straight over; only the screen changes.

An everyday picture, and where it breaks

A console program is a person at a counter: they say something, listen, and reply.

Where it breaks: the person at the counter hears a whole sentence. The program only holds what it was told to store, so an answer that is not stored in a name is gone.

Try it yourself (5 minutes)

  1. Run name = input("Name? ") then print("Hi " + name). Click the line in the console, type, and press Enter.
  2. Take the space out of the quotes in "Hi " and run it again. Where did the space come from?
  3. Try print("Score: " + 5) and read the message. Then fix it with str(5).

Questions students ask

  • Why does the console look different from the turtle playground? Programs that do not use the turtle get a console instead of a drawing. If a program imports turtle, the drawing comes back.
  • Why quotes? Without quotes Python looks for a name. With quotes it uses the text itself.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Numbers from text

Turn typed text into numbers, and check it first so a typo does not crash the program.

Numbers from text in 5 minutes (read before teaching this unit)

The idea in plain words

What input() gives back is always text, even when someone types digits. "5" + "5" is "55". To calculate, turn the text into a number with int("5").

int("twelve") stops the program with an error, so a careful program checks first. text.isdigit() asks the text whether every character is a digit and gives back True or False. The dot means "ask this text". An if uses the answer: convert inside the if, and reply politely in the else.

Why programmers use them

  • What people type is not what you expect. Checking input before using it (validation) is one of the most important habits in programming.
  • Real apps do this on every box. A form that says "Please enter a valid quantity" asked a question like isdigit() first.
  • A crash is a bad reply. The else turns it into a helpful one.

An everyday picture, and where it breaks

A door with a lock that asks "is this a key?" before turning.

Where it breaks: isdigit() only checks the shape of the text. 0 is all digits, so it passes even when 0 makes no sense. Checking that a number is sensible is a second question.

Try it yourself (5 minutes)

  1. Run a = input("A? "), b = input("B? "), print(a + b) with 5 and 5. Then wrap each input in int(...) and run again.
  2. Type twelve into the int() version and read the error.
  3. Add an if text.isdigit(): check around it and run it again with twelve.

Questions students ask

  • Can I type a decimal like 2.5? isdigit() says no (the dot is not a digit) and int() cannot read it. float() can, but this course sticks to whole numbers.
  • Why not just use try and except? That is a good second way, and it comes later. Checking first shows the idea in the simplest form.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Keep asking

Repeat with while, keep a running total, and read a whole guessing game.

Keep asking in 5 minutes (read before teaching this unit)

The idea in plain words

A while loop repeats for as long as a question is true: while guess != secret: keeps asking until the guess is right. It asks the question before every lap, so something inside the loop must change the answer, or the loop runs for ever (the playground's Stop button ends it).

+= adds to a variable: total += 5 is short for total = total + 5. Set the total to 0 once, before the loop, and it builds up as the loop goes.

Why programmers use them

  • You often cannot know how many times. A game, a PIN pad or a till keeps going until something happens.
  • A running total is everywhere. Scores, steps and shopping baskets all add as they go.
  • Longer programs are readable in parts. A guessing game is a set-up, a loop, a check and a count.

An everyday picture, and where it breaks

A while loop is "keep stirring while the sauce is lumpy". The cook checks before each stir, and stops when the answer changes.

Where it breaks: if nothing changes the sauce (you forget to stir), the cook checks for ever. In code, a line that asks again must be inside the loop, or the condition never changes.

Try it yourself (5 minutes)

  1. Run the guess-the-number game and count how many guesses you need. Halving the range each time always wins in five for 1 to 20.
  2. Move tries += 1 out of the loop (dedent it) and see what the program says.
  3. Change total = 0 to be inside the loop in the running-total program. What happens?

Questions students ask

  • for or while? for when you know how many times (four sides of a square); while when you are waiting for something to happen (the right guess).
  • What if it never stops? Press Stop. Then look for the line that should change the condition.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Menus and testing

A menu that keeps showing, and test cases written as a table.

Menus and testing in 5 minutes (read before teaching this unit)

The idea in plain words

A menu is while True:, a loop whose question is always yes, with break as the way out. Each lap shows the choices, asks for one, and runs one branch of an if / elif, with else for anything else. break leaves the whole loop, and the program carries on after it.

A test case is an input and the output you expect, written down before running. If the menu's replies come from a function, each test is one line: print(reply("9") == "Please choose 1 or 2.") shows True or False. A good list of tests includes the wrong inputs.

Why programmers use them

  • Menus are everywhere. ATMs, settings screens and text adventures are a loop, a decision and a way out.
  • Tests catch what you did not think of. Writing the expected output first stops you from deciding a wrong answer looks fine.
  • Functions make code testable. A function can be called with any input, with no typing.

An everyday picture, and where it breaks

A café counter: ask what they want, add it to the order, ask again, until they say that is all.

Where it breaks: a person at the counter knows when to stop. A program only stops when a line says so, so a menu with no break is a menu that never ends.

Try it yourself (5 minutes)

  1. In the till, type a few choices, then something silly, then pay. Watch which branch runs each time.
  2. Remove the break line, run it, and press Stop. What does that tell you?
  3. Write three test cases for the till (what to type, what it should reply) and run them.

Questions students ask

  • Is while True a mistake? Not when there is a break. It reads as "keep going until I say stop".
  • Why test with a function and not by typing? Typing the same choices every time is slow and easy to get wrong. A test line runs in a blink.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown

Console challenge

Build a console app with a menu, a checked number and a total.

Cheat sheet: Open to print (A4, with a picture of what each command draws) · Markdown