The standard input/output model and exact output
- Module
- M0.4
- Lesson
- 1 of 1
- Reading time
- 4 min
In this lesson
- Explain how the judge's standard input and standard output model works.
- Match your program's output to the judge's expected output: the same case, the same spacing, and nothing extra.
- Read a line of text with
input()and convert it to a number withint().
Every CCC problem follows the same shape: your program reads some text, works something out, and prints an answer. This lesson covers how that reading and printing works, and why the judge is unforgiving about what you print. The interpreter runs your program exactly as the module on what a program is described. This lesson adds the two streams of text it reads from and writes to while it runs.
Standard input and standard output
A judge does not open a file or ask you questions. It feeds your program a fixed block of text through standard inputThe stream of text a program reads its input from, normally whatever the judge feeds it.In the glossary, the stream a program reads from while it runs. Your program reads that text a line at a time and writes its answer to standard outputThe stream of text a program writes its output to, normally what the judge reads and compares.In the glossary, the stream a program prints to. The judge then reads standard output back and compares it with the answer it expects.
The whole block of standard input is ready before your program starts. Nothing you print changes what is waiting to be read. The problem statement describes what the input looks like, so you can plan your reads before you write a single line. A later module covers how the judge runs your program once for each test case and records a result for each one.
- Current
- Queued
- Done
- Not reached
- Just changed
Figure 1Two lines waiting on standard input, read one at a time
Read the steps as text
Standard input holds 2 line(s). Each call to input() takes the next line, without its newline, and the program stores it in a variable.
- The input waits on standard input, one line per row. Nothing has been read yet; line 1 of the program runs next.
- Line 1 ran:
input()reads the first line,'Maya', and drops its newline.namerefers to that text: it is a string, even if it looks like a number. Line 2 runs next. - Line 2 ran:
input()reads the second line,'5', and drops its newline.int()turns the text into the number5, andcountrefers to it. The program ends here.
input() reads the next line of standard input and gives it back as text, even when the line holds only digits. To use it as a number, convert it with int(). Nothing on standard input is used twice: once a line is read, the next call to input() moves on to the line after it.
n = int(input())print(n)Input
5Output
5n = int(input()) keeps the converted number under the name n, the same way any other assignment keeps a value under a name; the module on variables explains names fully. The program reads 5 as text, converts it with int(), and prints the number 5. Nothing else appears on standard output.
Exact output: no prompts, no extra text
Treat the comparison as exact: the safest habit is to print nothing the problem did not ask for, not even a single extra character. Judges differ in exactly how forgiving they are about small mismatches, so do not rely on one being lenient.
input() can take a prompt to show while it waits, something you may have seen in tutorials: input("Enter N: "). That prompt is not free. Python writes it to standard output before it reads anything, so it becomes part of what the judge compares.
n = int(input("Enter N: "))print(n)Input
5Output
Enter N: 5The printed output is more than the number 5. It starts with Enter N: , because that text was printed before the number. A judge expecting only 5 marks this as a wrong answer, even though a person reading the screen would understand it fine. Always call input() with nothing inside the parentheses in a submitted program.
Case matters the same way spacing does. If a problem expects the word yes in lowercase, printing Yes with a capital letter is a wrong answer, because the two pieces of text are not identical.
name = "Maya"score = 12print(name, score)Output
Maya 12print(name, score) joins the two values with a single space and nothing else, no matter how many values you give it. That plain form is the wrong tool whenever a problem's expected output uses a different separator, such as a comma. The module on output formatting covers how to write those instead.
Whitespace you cannot see is judged the same way as a letter you can see. A trailing space at the end of a line can be judged as a mismatch. So can an extra blank line at the end of your output, even though neither is visible on screen. Some judges' checkers overlook a trailing space; do not rely on that being true of every judge you submit to.
Every call to print() ends with a newline, whether or not you asked for one. Three separate print() calls produce three separate lines, each ending the way the judge expects, including the last one. You never need to add anything yourself to finish the final line of your output.
Putting it together
(nothing printed yet)- Just changed
Figure 2Reading a name and a count, then printing them on one line
Read the steps as text
The program reads a name and a count from two lines of standard input, then prints them together on one line separated by a single space.
- Python starts at the top of the file. Line 1 runs first.
- Line 1 runs:
nameis created with the value'Maya'.input()reads the first line and returns it as text:'Maya'. - Line 2 runs:
countis created with the value5.input()reads the second line, andint()turns the text'5'into the number5. - Line 3 runs: it prints
Maya 5.print(name, count)joins the two values with a single space. The program has finished: no lines are left to run.
The program reads two lines from standard input, one with input() alone and one converted with int(), then prints both values on a single line with print(name, count). Watch how the second input() call always reads the next unread line. It never rereads the first one.
A common mistake is calling input() more times than there are lines waiting. When there is nothing left to read, Python stops with an EOFError instead of returning an empty line, since there was never an empty line to return. A later module covers reading several values from one line, and another covers reading until the input runs out on purpose.
Try it: run the program yourself with your own name and a different count, and predict the printed line before you check it.
Nothing here needed a loop or a comparison; those come later. Reading a line, converting it, and printing it back with nothing extra is already enough to answer some of the simplest CCC problems.
This lesson covered the standard input and standard output model, and reading a line with input() and converting it with int(). It also covered why the judge needs your output to match its own: the right case, the right spacing, and nothing extra.