In the last lesson we discussed user input, but did not really explain how user input is obtained. In Python, the user types one line of input at a time. You should use the
function to actually obtain the next line of input from the user. The
input() function takes no arguments and always gives back a
| On this website, all input must be specified before the program runs. If you run Python interactively at home then |
Also, when working interactively, you can take advantage of the fact that
number = input("Enter a number between 0 and 100. ")
Here is an example of using
input() to get input. The grader will automatically specify the input for the program.
The next example demonstrates:
- By calling
input()multiple times, you can access multiple lines of input. The first call to
input()gets the first line, the second gets the second line, et cetera.
- The string given by
input()can be converted to an
float(like lesson 4)
- In the second test case, the third line of input is not read, because
input()is only called twice.
From now on, most exercises allow the option of entering your own test input. Try the following experiment: press the Enter input button above. Leave the input text box empty. Then, press Run test. You should get an error like
EOFError: EOF when reading a lineThe acronym EOF stands for End Of File. This message literally means that the program called
input()but failed to have any available input to read.
- Usually in our lessons, input is provided automatically by the grader, so this error could mean that your program called
input()too many times, running out and going past the end of the grader's input.
- However, in the example error you just caused, the input was user-provided, and you chose to provide no input, so the first call to
input()was already too much.
For the next exercise, you are asked to debug a program which is not working, and make it work. Note that the bug is not a typo, but rather a logical bug: the program was not correctly designed to do its job, so you must redesign it a bit.
You can continue to the next lesson, which is about