Learning Python With Program Templates: The Input One, Process One Template
A fun way to implement loops in your programs
In previous articles, I’ve discussed program templates for entering data, entering, processing, and outputting data, and making decisions in programs by selecting from alternatives. In this article, I’ll discuss a program template for writing programs that receive data input and then process that data before moving on to new data.
This template is called Input One, Process One. This template should be used when your program cannot receive all the data before performing processing or when you just want the program to input and process data elements one at a time.
You can compare this template with an earlier template we looked at: Input, Process, Output. This template is used when you want to get all the data into your program before performing any processing and output. With the Input One, Process One template, output happens during the processing step.
The Input One, Process One template is implemented using a looping statement. I will demonstrate the template using Python’s for statement and in a separate article that discusses implementing a different template, I’ll cover Python’s while statement for implementing loops.
The for Statement
The for statement is a looping construct that instructs a program to repeat a set of programming instructions a set number of times. There are several ways to write for statements in Python. I’ll start by showing you one syntax template:
for var in range(start, end):
do somethingThis syntax template uses the built-in range function. This function allows you to specify a range of numbers that represent the number of iterations (loops, or executions) you want with the for statement. For example, if I want the for statement to iterate ten times, I can use the range (1,11). Even though I want only ten iterations, I have to specify 11 as the stopping point of the range.
Now, let’s look at how the for statement works. This first example prints the numbers 1 through 10:
for i in range(1, 11):
print(i)The for statement takes each value of the range (1 through 10) and assigns it to the variable i. Then the statement inside the for is executed, causing the value of i to be printed on the screen.
Now that we’ve reviewed how this version of the for statement works, let’s see how to use it to implement the Input One, Process One template.
Implementing the Input One, Process One Template
The pseudocode for the Input One, Process One template is written this way:
Repeat the following steps:
Input data
Process the dataIn practice, the number of times the loop iterates will often be known in advance. When it’s not, another loop statement, the while statement, should be used.
Now it’s time to look at an example. In an earlier article, on the Select From Alternatives template, I used an example where a numeric test score was assigned a letter grade. Let’s use that example to demonstrate how to use the Input One, Process One template.
The example prompts the user to enter five numeric grades using a for statement. After the grade is entered, a letter grade is displayed. Here’s the program:
for i in range(1,6):
numGrade = int(input("Enter the test grade: "))
if numGrade >= 90:
print(numGrade,"is an A.")
elif numGrade >= 80:
print(numGrade,"is a B.")
elif numGrade >= 70:
print(numGrade,"is a C.")
elif numGrade >= 60:
print(numGrade,"is a D.")
elif numGrade < 60:
print(numGrade,"is a F.")
else:
print(numGrade,"is not a valid grade.")Here is the output from one run of the program:
Enter the test grade: 56 56 is a F. Enter the test grade: 78 78 is a C. Enter the test grade: 82 82 is a B. Enter the test grade: 99 99 is an A. Enter the test grade: 66 66 is a D.
Changing a String to Uppercase
For another example of the Input One, Process One template, let’s write a program that takes a string in lowercase letters and converts it to uppercase letters. To perform this task, we must recognize that a string can be broken down to its individual characters much like the range function breaks a range up into its individual numbers.
To demonstrate how a string can be broken up into individual characters, the following program takes a string and prints each character on its own line:
for i in “hello”:
print(i)This short program produces this output:
h e l l o
Now we’re ready to write the program to change a string from lowercase to uppercase. First, though, I need to introduce you to the function that converts a lowercase character to an uppercase character — upper.
The function works by attaching it to the end of a character. When invoked, the function returns the character as an uppercase character. Here is an example using the Python shell:
>>> “a”.upper()
‘A’Now we’re ready to write the program. Here it is:
lowercase = input("Enter a string: ")
for letter in lowercase:
print(letter.upper(), end="")Here is the output from a few runs of this program:
Enter a string: hello HELLO Enter a string: hello world HELLO WORLD Enter a string: supercalifragiliciousexpealidocious SUPERCALIFRAGILICIOUSEXPEALIDOCIOUS
Final Thoughts
The Input One, Process One template is just one of several templates that utilizes a looping statement for its implementation. We implemented this template using a for statement.
In my next article, I’ll introduce a new program template, Input and Process Until Done, and introduce the other major looping construct in Python — the while statement.
Thanks for reading. Please leave your comments and suggestions.
