Lesson 14
input() - reading text
After this lesson: Your program will read text from the input and use it in its answer.
Video
The video for this lesson is coming soon.
Explanation
So far programs always printed the same thing. Now we'll teach them to listen - the program will read data and answer it!
Input and output
Every program in a task has two sides:
- input - the data the program gets (e.g. a name, numbers),
- output - what the program prints with print.
The same program can get different data and answer differently each time - like a calculator that calculates whatever you type. The judge runs your program many times, each time with different data.
input() - the program asks, you answer
input() reads one line from the input. It's best to save it straight away in a variable:
text = input()
What happens in this one line - step by step:
- The program reaches
input()and waits. - You (or the judge) type a line of text and press Enter โ.
input()gives back that line as text, and the=sign saves it in the variabletext.- The program carries on - and can use the variable
text, e.g. inprint.
In the examples below you'll try it yourself: the program stops at input(), and you type the text on the screen. โจ๏ธ
Remember Enter! โ
When the program waits at input(), type your text on the screen and press the Enter key โ (or click the yellow โ Enter button next to it). Only then does the program get your text and go on. Without Enter the program keeps waiting.
Bajtek here! ๐ค In code these 4 steps are one line: text = input(). We read it from the right - first input() waits and gives back the text (e.g. "Ala"), then the = sign saves it in the variable text. From now on text remembers "Ala", and print can show it on the screen!
Your first input() โจ๏ธ
1) Click the arrow. 2) At input() the program stops - type something on the screen. 3) Press Enter โ. 4) The last line prints your text.
Click the arrow. First input() starts, then it waits until you type something on the keyboard.
1print("Type your first text:")2text = input()3print("You wrote:", text)
โThe parrot repeats ๐ฆ
Type anything and press Enter โ - the parrot repeats it twice.
Click the arrow. First input() starts, then it waits until you type something on the keyboard.
1word = input()2print(word)3print(word)
โA greeting
Type your name ๐ง and press Enter โ.
Click the arrow. First input() starts, then it waits until you type something on the keyboard.
1name = input()2print("Hi,", name + "!")3print("Nice to meet you,", name)
โA Christmas card ๐
Type a name ๐ง and press Enter โ - the program puts it into the wishes.
Type a name on the screen and press Enter - we'll make a card with it.
1name = input()2print("*" * 20)3print("Merry Christmas,", name + "!")4print("*" * 20)
โSeveral input() calls in a row
Every next input() reads the next line - like taking cards off the top of a pile. The first input() takes the first line, the second one the second line, and so on. That's why you press Enter after each one.
The input belt
The program waits for two lines: first a name ๐ง, then an animal ๐ถ. Press Enter โ after each one. Every input() takes one line.
Click the arrow. Every input() takes one line - in turn, from the top.
1name = input()2pet = input()3print("Hello,", name)4print(name, "has a pet:", pet)
โThe same program, different data
The same program again - this time type a completely different name ๐ง and animal ๐ถ. Press Enter โ after each one. The code is the same, but the result is different! That's how the judge runs your program with different data.
Click the arrow. Every input() takes one line - in turn, from the top.
1name = input()2pet = input()3print("Hello,", name)4print(name, "has a pet:", pet)
โFavourite things
The program asks for three things in turn: a favourite animal ๐ถ, colour ๐จ and food ๐. Press Enter โ after each one.
Click the arrow. Every input() takes one line - in turn, from the top.
1animal = input()2colour = input()3food = input()4print("I like", animal)5print("My colour is", colour)6print("For dinner:", food)
โA name badge ๐ท๏ธ
Type a name ๐ง, then a class ๐ซ (e.g. 5b). Press Enter โ after each one.
Type a name, then a class - each on its own line. Then we'll make a badge.
1name = input()2form = input()3print("+--------------+")4print("| " + name)5print("| class " + form)6print("+--------------+")
โA story from three words ๐
Type three lines: a hero ๐ (e.g. dragon), a place ๐ฐ (e.g. castle) and an object ๐๏ธ (e.g. golden key). Press Enter โ after each one.
Type a hero, a place and a thing - each on its own line. A story will be made from these three words.
1hero = input()2place = input()3thing = input()4print("Once upon a time", hero, "lived in", place + ".")5print("One day he found", thing + "!")
โJoining and repeating text
You can do two tricks with text:
+joins texts without a space:"Algo" + "Baza"givesAlgoBaza, andname + "!"adds an exclamation mark.*repeats text:"ha" * 3giveshahaha.
Compare with a comma in print: a comma adds a space, a plus doesn't.
Plus and times on text
Type a short word (e.g. ha) and press Enter โ - see what + and * do with it.
Type a word on the screen and press Enter - you'll see what +, * and a comma do with it.
1word = input()2print(word + "!")3print(word * 3)4print(word, "!")
โJoining two words
Type two words, each on its own line (e.g. Algo and Baza). Press Enter โ after each one.
Type a word on the screen and press Enter - you'll see what +, * and a comma do with it.
1first_word = input()2second_word = input()3print(first_word + second_word)4print(first_word, second_word)5print(second_word + first_word)
โThe input is the line Bajtek. What does the program print?
name = input()print("name")
The input has the lines Ala and Ola. What does the program print?
first_word = input()second_word = input()print(second_word + first_word)
What does print("ab" * 2 + "!") print?
Text from input can be repeated
Type a word and press Enter โ - the text you read is ordinary text, so + and * work on it.
1word = input()2print(word * 3)3print(word + "!" + word)4print("-" * 10)
โJoining with input
If the word "ole" was read, the expression word + "!" + word works out like this:
The input is two lines: Cat and Dog. What does the program print?
a = input()b = input()print(b, a)
The input is one line: 7. What does print(input() * 2) print?
Bajtek shouts in the mountains ๐ฌ
Click Next and see how + and * make an echo out of a word.
Code
๐ฅ๏ธ Screen
At the top
Bajtek climbs to the top of a mountain โฐ๏ธ and wants to shout. The program asks: word = input(). Bajtek types hop and presses Enter - the text "hop" goes into the box word.
An echo in the mountains โฐ๏ธ
Shout something in the mountains: type a word (e.g. hop) and press Enter โ.
Type a word on the screen and press Enter - you'll see what +, * and a comma do with it.
1word = input()2print(word + "!")3print(word * 2 + "!")4print(word * 3 + "!")
โRemember
input()reads one line from the input - always as text.- Every next
input()reads the next line. - In tasks write just
input()- no text in the brackets. +joins texts without a space,*repeats them.
In the tasks the examples show what the input will be. Your program has to work for any data - not just the example!
Tasks from the video
We solve them together in the video - pause it and try first on your own.
Practice tasks
Try to solve them without the video. Stuck? Every task has hints and an editorial.
Sign in to save your progress.