Course 2.1Lesson 1 of 4
The input() function shows a question, waits for the user to type an answer and press Enter, and gives back what was typed. The text in the brackets is the question. We keep the answer in a variable. On this site, the answers are typed in the Input box, one on each line, before you press Run, and each input() reads the next line. If the Input box has too few lines, the program stops with an EOFError.
Try the code and the simulations beside the lesson.
After this lesson you can
Until now, your programs did the same thing every time. A program that asks a question is different. It can give a different answer to a different person. It is the first step towards programs that are useful in real life: a bank, a game, a calculator, a form on a website, all of them ask.
In Python, the function for this is input().
Before you run it, look at the Input box under the code. If you cannot see it, press + Input. Type a name there, for example Asha. Then press Run.
What happened?
input("What is your name? ") shows the question and waits for an answer.name, because of name = ... on the left.On your own computer, the same program waits in a terminal until you type the answer and press Enter. The idea is the same.
The text inside the brackets is called the prompt. It is shown to the user so that they know what to type. It is not the answer. A good prompt says what is wanted, and ends with a space or a colon so that the answer does not touch it, for example "Your age: ".
input() with nothing inside also works. It waits, but shows no question, and a user does not know what to do. Always write a prompt.
Each input() reads the next line of the Input box. The first input() reads line 1, the second reads line 2, and so on.
Type two lines in the Input box, Asha and then Rao. Run it. Now swap the two lines in the Input box, and run it again. The program does not know which is a first name. It only reads the lines in order. This is why a good prompt matters, and why the order of the lines in the Input box matters.
The last line uses first[0], the first letter of the text. We study this in the course on strings.
Each input() takes the next line of the Input box, from the top. That is why the order of the lines matters.
What if the program asks two questions and you type only one line? The second input() has nothing to read. Python stops with an EOFError: EOF means "end of file", the end of the answers. The fix is to type enough lines. Count the input() calls in the program, and type that many answers.
input().input() reads the first line.input("Name? ") alone asks, but the answer is lost. Write name = input("Name? ").Asha, not "Asha". The marks would become part of the text.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
input("question") shows the question, waits for an answer and gives the answer back.name = input("Name? ").input() reads the next line. Too few lines give an EOFError.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
input() called?input() calls. How many lines must the Input box have?input("Your age? ") on a line by itself?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.