Track: Python for Kids · Lesson 4 of 10 · Ages: 8+ · Time: 35 minutes
So far your programs have talked and never listened. Today that changes! You will write programs that ask a question, wait for you to type an answer, and then do something with it.
Beep says I love questions. When I ask “How full is my battery?”, somebody has to tell me the answer! In Python, the word that listens is input. Beep boop!
What you need
- Python and IDLE.
- A new file called
questions.py.
Grown-ups When a program runs, the questions and the typed answers both appear in the Shell window. Click in that window before typing. All the examples here use playful prompts (robot names, favourite foods, number guesses). Please encourage children to type silly made-up answers and never real personal details such as their full name, age, school or address, even in a practice program.
Your first question
The word input shows a question and waits for an answer. Whatever you type is stored in a variable:
robot_name = input("What is your robot called? ")
print("Hello, " + robot_name + "!")
print("What a great name!")
When you run it, the conversation looks like this. The word after the question is what you type, and we pretend you typed Zippy:
What is your robot called? Zippy Hello, Zippy! What a great name!
Let us look at the first line. Inside the brackets of input( ) is a string, which is the question to show. We put a space at the end of the question so that your typing does not squash up against the question mark. Whatever you type is stored in the variable robot_name. After that we can use it, like any other variable.
Pixel says Run it again and type something different. A good program works for any answer, not just the one you were thinking of. Meow!
A silly story maker
You can ask several questions and put the answers into a story. Programmers call this a “fill in the blanks” program:
colour = input("Pick a colour: ")
animal = input("Pick an animal: ")
food = input("Pick a food: ")
print("Once upon a time, a " + colour + " " + animal + " found a giant " + food + ".")
print("The " + animal + " ate the whole " + food + " and said, 'Yum!'")
If you typed purple, penguin and pizza, you would see:
Pick a colour: purple Pick an animal: penguin Pick a food: pizza Once upon a time, a purple penguin found a giant pizza. The penguin ate the whole pizza and said, 'Yum!'
Careful: input is always text!
Here is a tricky thing that surprises every beginner. Whatever you type, Python treats it as a string, even if it looks like a number. Watch what happens when we try to add two numbers:
first = input("First number: ")
second = input("Second number: ")
print(first + second)
You type 3 and 4, and the computer answers:
First number: 3 Second number: 4 34
Not 7! Remember from lesson 2 that + joins strings together. Python saw the text "3" and the text "4" and glued them into 34.
To tell Python “this is a real number, please do maths with it”, wrap the answer in int( ). The word int is short for integer, which is a whole number like 3, 10 or 0.
first = int(input("First number: "))
second = int(input("Second number: "))
print(first + second)
First number: 3 Second number: 4 7
Now int turns each answer into a number first, so + really adds. Read it from the inside out: input(...) collects text, and int(...) changes that text into a whole number.
Did you know? If you type words when the program expects a number, int cannot work it out and Python stops with a message called ValueError. A program that handles silly answers politely is something you will learn to build later.
Using the number
Once an answer is a real number, you can do anything with it that you learned in the last lesson:
number = int(input("Pick a number from 1 to 10: "))
print("Double your number is", number * 2)
print("Your number plus ten is", number + 10)
print("Your number times itself is", number * number)
Pick a number from 1 to 10: 8 Double your number is 16 Your number plus ten is 18 Your number times itself is 64
Here is a robot snack-bar. It asks how many batteries are wanted, then works out the cost. Each battery costs 3 coins:
batteries = int(input("How many batteries would you like? "))
cost = batteries * 3
print("That will be", cost, "coins. Beep boop!")
How many batteries would you like? 5 That will be 15 coins. Beep boop!
Challenges
Try it Write a program that asks “What is your favourite animal?” and “What sound does it make?” and then prints a sentence such as “The cat says meow!” using both answers.
Show the answer
animal = input("What is your favourite animal? ")
sound = input("What sound does it make? ")
print("The " + animal + " says " + sound + "!")
If you typed frog and ribbit, it prints The frog says ribbit!
Try it Write a program that asks for two numbers and prints the answer when you multiply them.
Show a hint
Use int(input(...)) twice, once for each number. Then print the first one times the second one.
Show the answer
first = int(input("First number: "))
second = int(input("Second number: "))
print("The answer is", first * second)
If you typed 6 and 7, it prints The answer is 42.
Try it Change the snack-bar so that each battery costs 4 coins, and add a second question that asks how many cookies are wanted at 2 coins each. Print the total.
Show the answer
batteries = int(input("How many batteries would you like? "))
cookies = int(input("How many cookies would you like? "))
total = batteries * 4 + cookies * 2
print("That will be", total, "coins.")
With 3 batteries and 5 cookies, the total is 3 times 4 plus 5 times 2, which is 22 coins.
What you learned
input("question ")asks a question and waits for an answer, and you store the answer in a variable.- Everything you type comes in as a string, even digits.
int( )turns text such as"7"into the real number 7, so you can do maths with it.- Programs that ask questions should use playful made-up answers, never your real personal details.
