← Console bridge: text in, text out

Check first

Ask the typed text whether it is all digits with isdigit, and use an if to choose, so a typo gets an answer instead of a crash.

Type a word into a quantity box and a good app says so politely. A careless one crashes. The difference is one question asked before the number is used.

Last lesson int() turned typed digits into a number. But int("twelve") stops the program with an error. A careful program checks first, and chooses what to do.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.py
text = input("How old are you? ")
if text.isdigit():
    age = int(text)
    print("Next year you will be " + str(age + 1) + ".")
else:
    print("Please type your age as a number.")

You know if and else. Read the code, including the new text.isdigit(). What happens if you type twelve?

You run it and type twelve. What happens?

RunRun it and compare the result with your prediction.

Run

Run it three times: with 12, with twelve, and with -3.

Line What it does Why
if text.isdigit(): Asks the text: are you made only of digits? The dot means "ask this text". The answer is True or False, which an if can use
age = int(text) Turns the text into a number Only runs when the check said yes, so it cannot fail
else: Runs when the check said no The person gets a reply instead of a crash

A minus sign is not a digit, so -3 fails the check. That suits an age.

lesson.py
text = input("How old are you? ")
if text.isdigit():
    age = int(text)
    print("Next year you will be " + str(age + 1) + ".")
else:
    print("Please type your age as a number.")
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.py
text = input("How old are you? ")
if text.isdigit():
    age = int(text)
    print("Next year you will be " + str(age + 1) + ".")
else:
    print("Please type your age as a number.")

Four answers, four paths.

The check is there, but it comes too late.

For each answer typed, fill in what text.isdigit() gives and what the program prints.

typedtext.isdigit()prints
12TrueNext year you will be 13.
twelve
9
4.5

The program has a check, but typing twelve still crashes it. Click the line that is in the wrong place.

ModifyChange the code on purpose and check the result.

Modify

Change the program so it asks for two numbers, checks both, and prints their total. If either answer is not a number, print a message instead.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Ask for two answers, store them as text, and check both: first.isdigit() and second.isdigit().

Another hint

Inside the if, convert each with int and print the total.

Ask for two numbers, check both, and print their total.

    Run your program. This ticks when your code does all of the above.

    InvestigateTrace how each line of code connects to what you see.

    Mix it up

    This mixes the last lesson and if: a typed number, converted, and used.

    lesson.py
    text = input("How many tickets? ")
    if text.isdigit():
        print("That will be $" + str(int(text) * 12) + ".")
    else:
        print("Please type a number.")
    
    The person types 0. What does the program print?

    MakeBuild something new using what you have learned. Expect a few tries; that is normal.

    Make

    Tickets to the school play cost $12 each. Write a ticket machine that asks how many tickets, checks the answer is a number, and prints the total, such as That will be $36. If the answer is not a number, it prints a helpful message. Test it with 3, with 0 and with three.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Store the answer as text first, then check it, then convert it inside the if.

    Another hint

    Put the cost calculation inside the if, and a helpful message in the else.

    Ask how many tickets, check it with isdigit, and print the total cost.

      Run your program. This ticks when your code does all of the above.

      For teachers

      Students learn to check typed text with isdigit before converting it, so a typo gets a reply, not a crash. About 30 minutes.

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      Running the lesson

      1. Predict: take a vote before running. The wrong options are three real beliefs: int reads words, the crash happens anyway, and nothing happens. Ask the 'error' voters what they think isdigit does, and the '13' voters whether Python can read words.
      2. Run: run with 12, twelve and -3. Ask which path each took.
      3. Investigate: the bug is about order. Ask a student to say what the program does first.
      4. Make: students test with a number, zero and a word. Ask them to try something to break it (the empty answer, a space).

      The hard part (on purpose)

      Putting the conversion before the check. Ask which line runs first.

      When students get stuck

      Stretch and support

      Stretch: What does the check say for an empty answer? Try it and fix the message.

      Support: Give the if / else with the middle lines missing and ask the student to complete them.

      Where it leads

      Next lesson: instead of ending after one bad answer, the program keeps asking until it gets a good one.

      What each step is for

      Now you can
      Next lesson6. Repeat untilKeep running lines with while for as long as a condition is true, so a program can keep asking until it gets what it needs.