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
| Word | What it means | Example |
|---|---|---|
| Input | Data that goes into a program. | what you type |
| Output | Data that a program shows or gives out. | text on the screen |
| Keyboard | The device you type on. It gives input. | typing a name |
| Screen | The device that shows output. | text from print() |
| Prompt | A message that tells the user what to type. | "Age? " |
| String | Text inside quote marks. | "Sam" |
| Integer | A whole number. | 15 |
| Float | A number with a decimal point. | 2.5 |
| Variable | A name that holds a value. | name |
| Exception | An 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
- 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.
- 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.
- 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, ".
- 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.
- 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.
- 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
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
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 questionsWorked example: follow the program
| Step | What happens | age holds | On screen |
|---|---|---|---|
| 1 | Python shows the prompt Age? | nothing yet | Age? |
| 2 | The user types 15 and presses Enter | "15" (a string) | |
| 3 | int() changes the text to a number | 15 (an integer) | |
| 4 | age + 1 is calculated and printed | 15 | Next 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
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.
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 questionsRemember
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.