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

Preview

Input from the keyboard, output to the screen

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

45 minutes

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.

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.

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.

input() always gives a string

data-callouts.warning.label

Whatever the user types, input() gives it back as text. The number 15 arrives as "15". To calculate with it, change it with int() or float().

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

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

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 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.

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.