View as PDF

COMPUTING AND DIGITAL LITERACY

Input and Output

Difficulty level: Easy

Computing and Digital Literacy

This document was made by AI and there may be mistakes. If a question or answer looks wrong, use the Flag a mistake button to let Brett know so he can fix it.

Overview of the concept

This is how a program takes what you type or tap and gives something back, like a calculator app turning your numbers into an answer on screen. Every program is a loop of input and output, you give it something, it does the work, and it shows you the result. That is how a search box turns your words into results, how a game turns your button presses into movement on screen, and how a form turns your details into a booking.

Worked examples

Example 1

Greet by name

Solution

name = input("What is your name? ")

print("Hello, " + name)

User types Sam -> Output: Hello, Sam

Ask for the value with input() and store the answer in name.

Join the greeting text and the stored name with +.

Print the result so the user sees: Hello, Sam.

Example 2

input() reads an age as the text '14'. How do you turn it into a number, and what is the age next year?

Solution

age_text = input("Age? ") ->"14"

age = int(age_text) -> 14

Next year: age + 1 = 15

Read the age with input(), which always gives text, here "14".

Convert it with int() so it becomes the number 14.

Now arithmetic works: 14 + 1 = 15. Without converting, "14" + 1 would fail or join wrongly.

Practice questions

1

name = input('What is your name? '); print('Hello, ' + name). The user types Ana. What prints?

2

first = input('First name? '); last = input('Last name? '); print(first + ' ' + last). The user types Ana then Lee. What prints?

3

What does the text inside input('Favourite colour? ') do?

4

age_text = input('Age? '); age = int(age_text); print(age + 1). The user types 21. What prints?

5

age_text = input('Age? '); print(age_text + '1'). The user types 9. What prints?

6

num_text = input('Give a number: '); num = int(num_text); print(num * 2). The user types 7. What prints?

7

How do you turn the text '27' into the number 27?

8

A program asks for a name and prints a greeting. Which part is input?

9

What does input() always return?

10

Why convert input before doing maths on it?

ANSWER KEY

1. Hello, Ana

2. Ana Lee

3. It shows a prompt asking the question

4. 22

5. 91

6. 14

7. int("27")

8. The name the user types

9. Text (a string)

10. Because input arrives as text, and maths needs numbers