AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Variables, input and casting

Week 2 · Unit 7.1: Block it out · 3 lessons
2

Questions for the user

Computing 7: Unit 7.1, pp. 8–43
GoalUse input to store what the user types, and sort data into the types string, integer and real.
New words
input · a command that shows a question, waits for the user to type, and gives back what was typedstring · text: letters, digits and symbols, written in quotation marksinteger · a whole number, such as 24 or −10real (float) · a number with a decimal point, such as 2.75; Python calls it float
Explanation

The input command lets the user give the program data while it runs. The words in the brackets are the question, and the answer is stored in a variable. Data comes in types: a string is text, an integer is a whole number and a real number has a decimal point. Python calls the real type float. A phone number is a string, because you never add phone numbers together. Remember: input always gives back a string, even when the user types digits.

Python
1name = input("What is your name? ")
2print("Hello", name)
Output
Hello Aisha
Worked examples
whenclickedaskWhat is your name?and waitsayjoinHello answer
This Scratch program and the Python program above do the same job. The answer block in Scratch matches the variable in Python. Aisha types her name when the question appears.
Python
1pet = input("Name your pet: ")
2animal = input("What animal is it? ")
3print(pet, "is a", animal)
Output
Biscuit is a hamster
A program can ask several questions. Each answer needs its own variable. The user types Biscuit and then hamster.
ValueData typeWhy
"Nairobi"stringtext in quotation marks
24integera whole number
2.75real (float)it has a decimal point
"24"stringquotation marks make it text
−8integera whole number can be negative
Sort the data by type. A value in quotation marks is always a string, even when it looks like a number.
Class activity

Question cards (no computer): write three questions on cards, for example ‘Favourite fruit?’, ‘How many brothers and sisters?’ and ‘How tall are you in metres?’. Put three paper cups next to the cards and label them fruit, family and height. A partner is the program: they read a card, you answer, and they write your answer on a slip and drop it in the right cup. Then sort the slips into three piles: string, integer, real. Which pile did each slip go in? If you have a computer or tablet, type the program for one question into IDLE with an adult.

Practice
1
Give the data type of each value in the table, and a reason.
ValueData type
"Lima"?
14?
3.75?
"42"?
−8?
2
The user types Lima and then Friday. What does the program show?
Python
1town = input("Town? ")
2day = input("Day? ")
3print(day, "in", town)
3
Write a Python program that asks ‘How old is your pet?’ and then shows ‘Your pet is’, the answer and ‘years old’.
4
Write this Scratch program in Python.
whenclickedaskFavourite fruit?and waitsayjoinI like answer