Learning Python With Program Templates: Select From Alternatives (Part 1) | by Mike McMillan | May, 2022

A fun way to create a flexible program

Photo by Alex Kotliarskyi on Unsplash

In this article, I’m going to introduce the Select From Alternatives program template. This template provides the programmer with a process for making decisions in their programmes. The template is implemented using Python’s if statement.

Many programming languages ​​also provide the case statement as a way of implementing this template, but Python does not have the case statement, so the if statement will have to do.

Before I introduce the template, however, I need to cover some preliminaries for use if statements, primarily the relational operators. In my next article, I’ll continue discussing this template but in the context of working with ranges of values ​​that will require the use of the Boolean operators.

Most problems that you want to solve with a computer program require comparing values ​​to determine what the program should do. For example, in a payroll program, the program must examine an employee’s time sheet to determine if the program is to pay the employee straight time for working forty hours or less or pay overtime if the employee worked more than forty hours.

To compare values ​​we must use one of the relational operators. Here is a list of these operators:

  • > Greater than
  • >= Greater than or equal to
  • < Less than
  • <= Less than or equal to
  • == Equal to
  • != Not equal to

These operators are binary operators, which means that there must be a value on both sides of the operator. The value can be either a variable or a literal. An expression that contains a relational operator and two values ​​is called a relational or conditional expression.

Here are some examples:

100 > 50
"Joe" == "joe"
salary < 50000
hoursWorked <= 50
(regularPay * 1.1) > 1000

A relational expression returns a Boolean value. The Boolean values ​​are True and False. This is how Python makes decisions in a program. A relational expression is written and if the result is Truethe program goes in one direction and if the result is Falsethe program goes in another direction.

To make this happen, you must use a relational expression with an if statement. I’ll discuss how to use this statement next.

Python’s if statement is used to make decisions in programs. The if The statement analyzes a relational expression and then executes one set of code or another, depending on the result of the relational expression.

There are several forms of if statements. They are: 1) the simple if statement; 2) the if-else statement; and 3) the if-elif statement.

Let’s start by looking at the syntax template for the simple if statement:

if relational-expression:
statement(s)

There are two things to note about this template. First, note the colon right after the relational expression. This colon is necessary and leaving it out will result in an error. Also note that the term statement(s) indicates that there can be one or more than one program statement that is to be executed if the relational expression results in a True value.

Let’s look at an example. This example demonstrates how to check to see if a number is even. The program uses the modulus operator to check to see if there is a remainder when the number is divided by two. If there is no remainder, the number is even. If there is a remainder, the number is odd. Here’s the code:

number = int(input("Enter a number: "))
if number % 2 == 0:
print(number,"is even.")

Here is the output from two runs of this program:

Enter a number: 22
22 is even.
Enter a number: 133
133 is odd.

Notice that nothing happened when an odd number is entered. I’ll fix that immediately below when I discuss the if-else statement.

Most of the time you want your program to execute one set of statements if a relational condition is true and another set of statements if the condition is false. To do that, we need to extend the if statement to include an else clause, which causes an alternative set of statements to execute. This statement is called the if-else statement.

Here is the syntax template for the if-else:

if relational-expression:
statement(s)
else:
statement(s)

Let’s illustrate how the if-else statement works by modifying our program above for determining if a number is even to also having the program identify odd numbers. Here’s the program:

number = int(input("Enter a number: "))
if number % 2 == 0:
print(number,"is even.")
else:
print(number,"is odd.")

Here is the output from two runs of this program:

Enter a number: 2452
2452 is even.
Enter a number: 1333
1333 is odd.

The if-else statement is a very common form of conditional statement and one you will use often in your programming.

There is one more form of if statement to discuss — the if-elif statement for making more than one if-else decision.

There are many times in a program when you need to make a choice from among multiple possibilities and the if-else statement is not powerful enough. Python has just the right statement for this — the if-elif statement.

Let’s start out with the syntax template for this statement:

if relational-expression:
statement(s)
elif relational-expression:
statement(s)

else:
statement(s)

The ellipsis (…) indicates that there can be multiple elif clauses in the statement.

Let’s look at an example to demonstrate how the if-elif statement works:

Here is the output from one run of this program:

Choose one of these things to do outside:
1. Go for a hike.
2. Play tennis.
3. Play golf.
Enter choice: 2

The tennis courts are two blocks away.

Now that we’ve discussed how to write if statements, let’s see how to use them to implement the Select From Alternatives program template.

This template is used to choose from a set of alternatives based on a given value. To be honest, I’ve already demonstrated how this template works using my example of the if-elif statement above, but I will demonstrate the template again with another example.

The following program has the user enter a numeric test score and the program determines the letter grade earned. Here is the program:

Here are a few runs of the program:

Enter the numeric score: 77
A grade of 77 is the letter grade C
Enter the numeric score: 80
A grade of 80 is the letter grade B
Enter the numeric score: 54
A grade of 54 is the letter grade F

This article demonstrates how to use the Select From Alternatives program template to make choices in your programs. In my next article, I’ll show you how to implement the template using more complex alternatives, such as choosing from ranges of values, which will involve using the Boolean operators.

Thanks for reading. Please leave comments and suggestions.

Leave a Comment