Menu

Trace Tables

Input and Output in Trace Tables

An algorithm that takes in data and displays results needs both input and output values recorded as you trace it. Input values arrive when an input statement runs, and output values appear only when an output statement runs.

Recording input

When you reach an input statement such as int(input()), take the next value from the supplied data and write it in that variable's column on the row where the input happens. The supplied data is used in order: the first input statement takes the first value, the second takes the second, and so on. Before a variable's first input row, its cells stay empty. Values are not collected at the start of the trace; a later input statement picks up the next unused value at the moment it runs, which matters when an input sits inside a loop.

Recording output

Write in the output column only on rows where a print() statement actually runs. Many rows produce no output at all, so those cells stay empty. A variable's value belongs in its own column; it goes in the output column only if the algorithm really displays it.

This program reads four scores, displays a message for each score of 10 or more, then displays how many there were:

count = 0
for entry in range(4):
    score = int(input())
    if score >= 10:
        print("pass")
        count = count + 1
print(count)

Output when the user types 12, 4, 15, 9:

12
pass
4
15
pass
9
2
Stepentryscorecountoutput
before loop0
10121pass
2141
32152pass
4392
after loop22

When the algorithm finishes, the values left in the output column are the final output, in the order they were produced. Here that is pass, then pass, then 2. The value 2 appears in the output column only because the last statement printed it.

Practise this