Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

2.5 (1 of 3): Input from the keyboard, output to the screen

The whole lesson, free. Read it and have a go at the activities. To work the tasks and labs in the real lesson, press Start lab, with no account needed.

45 minutes

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

A program that talks to you

A game asks for your name. You type it. The game says: Hello, Sam.

Two things happened. The program received data from you. Then it showed data to you. Every program that talks to people does these two jobs.

Key words

WordWhat it meansExample
InputData that goes into a program.what you type
OutputData that a program shows or gives out.text on the screen
KeyboardThe device you type on. It gives input.typing a name
ScreenThe device that shows output.text from print()
PromptA message that tells the user what to type."Age? "
StringText inside quote marks."Sam"
IntegerA whole number.15
FloatA number with a decimal point.2.5
VariableA name that holds a value.name
ExceptionAn error that stops a program while it runs.ValueError

The cells students fill in are left blank in this preview.

Watch: what you type, what it shows

Press Play on the short film below. A program asks for a name and waits. The film shows what the user types and what print() puts on the screen, then why typed digits are still text.

Film: keyboard input and screen output

Input from the keyboard, output to the screen

Every scene, as text

  1. A prompt is a message that tells the user what to type. name = input("Name? ") shows the prompt Name? on the screen, ending in one space, then waits for the user to type and press Enter.
  2. The user types Sam, underlined here, and presses Enter. input() gives back what was typed as a string, so name holds "Sam". The space at the end of the prompt keeps Sam apart from the question mark.
  3. print() shows output on the screen. print("Hello, " + name) shows Hello, Sam on the next line, under the line input() left. + joins text and adds no space: the space is the one inside "Hello, ".
  4. format() puts values into the {} gaps in the text, in order: the first gets team and the second goals. With team set to "Leeds" and goals set to 3, print("{} scored {} goals".format(team, goals)) shows Leeds scored 3 goals.
  5. Myth (false): input() gives a number when the user types digits. With age = input("Age? "), the user types 15, but age holds "15", a string. input() always gives text.
  6. Change the type first: age = int(input("Age? ")). int() changes "15" to the integer 15, so print(age + 1) shows 16.

Try it: print to the screen

Code: python
print("Hello")
print("Total:", 7)
print("Score: " + str(10))

team = "Leeds"
goals = 3
print("{} scored {} goals".format(team, goals))

# print() shows output on the screen.
# Change the team and goals and run again.

Make a free account to save your work and track your progress.

print() is output

print() shows something on the screen. You can print text, numbers and variables.

To join text and a number, change the number with str() and use +. Or use format(). It puts values into the {} gaps in the text, in order.

input() asks the user

Code: python
name = input("What is your name? ")
print("Hello, " + name)

age = int(input("Age? "))
print("Next year you will be", age + 1)

# Run this on your own computer or in the practice editor.
# input() waits for the user to type and press Enter.

Make a free account to save your work and track your progress.

input() always gives a string

Warning

Many people think input() gives a number when the user types digits. It always gives text, so 15 arrives as "15". To calculate with it, change it with int() or float() first.

What does it show?

Have a go

What does it show?

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Worked example: follow the program

StepWhat happensage holdsOn screen
1Python shows the prompt Age?nothing yetAge?
2The user types 15 and presses Enter"15" (a string)
3int() changes the text to a number15 (an integer)
4age + 1 is calculated and printed15Next year you will be 16

The cells students fill in are left blank in this preview.

What if the user types the wrong thing?

Users sometimes type the wrong thing. They type the word ten instead of 10. Then int("ten") stops the program with a ValueError.

You can catch the error with try and except. The program does not crash. It does something else instead.

Try it: catch the error

Code: python
text = "ten"

try:
    number = int(text)
    print("OK", number)
except ValueError:
    print("That is not a whole number")

# Change text to "10" and run it again.

Make a free account to save your work and track your progress.

Match the code to its job

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Your turn: talk to the user

Write four functions. The test checks the prompt exactly, with one space at the end. ask_name asks "What is your name? " with input() and gives back the text "Hello, " and the name, for example Hello, Sam. show_total prints Total: and the number, for example Total: 7. ask_age_next_year asks "Age? " and gives back the age plus 1 as a whole number. safe_int changes text to a whole number, or gives back None if it cannot. The tests type for you. Press Run Tests.

Code: python
def ask_name():
    # Ask "What is your name? " with input(). Give back "Hello, " and the name.
    return None


def show_total(total):
    # Show the text: Total: 7  (for a total of 7). Use print().
    pass


def ask_age_next_year():
    # Ask "Age? " with input(). The user types a whole number.
    # Give back the age next year as an integer.
    return None


def safe_int(text):
    # Change text to a whole number. If that is not possible, give back None.
    return "?"

Make a free account to save your work, or press Start lab to work through the whole lesson.

Key words crossword

Have a go

Loading…

Your answers are marked here and are not kept. A college account keeps every student’s marks.

Exit quiz

Have a go

Exit quiz

6 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Remember

Important

input() receives text from the keyboard. print() shows output on the screen. Change the type before you calculate. Use try and except for bad input.

Teach this lesson with your class

On a college account your students work the tasks and the labs as well, and every mark is kept for you.