Complex Python decision making

Jeremiah Taguta (BSc in Computer Science, Masters in Information Systems)
Published on 21-10-2023
Mastering complex Python decision making with If, Elif, Else, and Logical operators


Introduction

In our prevision article, we discussed simple decision-making whose conditions were only one relational operator. In this article, we shall dive a little deeper and use if and elif statements with complex conditions, thus having more than one relational operator joined by logical operators. Also to be discussed are the common errors in complex decision-making in Python. We shall also rewrite the grading and greatest of three numbers programs and programming practice questions.

The "and", "or", "nor", "nand", and many other logical operators, as discussed here, can be used to join two or more conditions, creating a complex condition based on the scenario at hand.

1. Grading system

Let's start by rewriting the system using complex decision-making.

It is a program where:

marks 80 - 100 is an A, 70 - 79.9... is a B, 60 - 69.9... is a C, 50 - 59.9... is a D, 40 - 49.9... is an E and 0 - 39.9... is an F.

Now, for a situation where a mark should be between x and y, the correct logic operator is the "and" operator, which returns True if both operands are True. 

For instance, for one to get an "A", the mark should be greater or equal to 80 but less than or equal to 100, as in line 2, thus, 85 is an "A". For a "B", a mark should be 70 and above but less than 80. It can't be less than or equal to 80 as 80 will fall in both categories A and B as it will satisfy both lines 2 and 4 conditions a situation which has to be avoided.

Any mark above 100 and less than 0 will be executed under the else statement as it won't be evaluated to True by any of our if and elif statements thus an "Invalid mark" will be displayed.

1  mark =  float(input("Enter a mark for grading :"))
2  if mark >= 80 and mark <=100:
3     print("A")
4  elif mark >= 70 and mark < 80:
5     print("B")
6  elif mark >= 60 and mark < 70:
7     print("C")
8  elif mark >= 50 and mark < 60:
9     print("D")
10 elif mark >= 40 and mark < 50:
11    print("E")
12 elif mark >= 0 and mark < 40:
13    print("F")
14 else:
15    print("Invalid mark")

2. Common errors in Complex conditions

When using complex conditions, one of the common mistakes is writing a condition as in line 1 below

1 mark = 85
2 if mark >= 80 and <= 100:
3    print("A")

When run, the following error will be displayed:

if mark >= 80 and <= 100:
                 ^
SyntaxError: invalid syntax

Take note that the logical operator, an "and" in this case, join individual relational operators hence mark is being compared to 80 only and not to 100 in the above example. Therefore, after the "and" operator we should put a mark <=100 as shown in line 1 below

1 mark = 85
2 if mark >= 80 and mark <= 100:
3   print("A")

3. Greatest of 3 numbers program

Remember the greatest of 3 numbers program which we wrote in our Python simple decision making article. We used nested if statements but now we want to write the same program using complex decision making. 

As said in that article, for the first number to be greatest, it has to be bigger than the second and third numbers. For the second number to be greatest, it has to be bigger than both the first and third numbers. Finally, for the third number to be greatest, it has to be bigger than both the first and second numbers. 

Having explained the logic, the following is the code for the program with comments explaining the conditions under which the if, elif and else statements' bodies are run or executed.

1  num1 = float(input("Enter first num: "))
2  num2 = float(input("Enter second num: "))
3  num3 = float(input("Enter third num: "))
4  if num1 > num2 and num1 > num3:
5     # executed if num1 is greater than num2 and num1 greater than num3
6     print(num1, "is greatest of",num2, "and", num3)
7  elif num2 > num1 and num2 > num3:
8     # executed num2 is greater than num1 and num2 is greater than num3
9     print(num2, "is greatest of",num1, "and", num3)
10 else:
11    # executed num3 is greater than num1 and num3 is greater than num1
12    print(num3, "is greatest of ",num1, "and", num2)

The above is an interactive program, the output will therefore depend on the user values. For instance, num1 = 5, num2 =10 & num3 = 7 will output 10, which is the greatest of, 5, and, 7.

4. Practice Questions

To enhance you skills, you may attempt the following questions and share a link (github, google drive e.t.c) to your solutions in the comments section then i may look at it and give reviews. 

  1. Write a program that asks the user how many credits they have taken. If they have taken 23 or less, print that the student is a freshman. If they have taken between 24 and 53, print that they are a sophomore. The range for juniors is 54 to 83, and for seniors it is 84 and over.
  2. A year is a leap year if it is divisible by 4, except that years divisible by 100 are not leap years  unless they are also divisible by 400. Write a program that asks the user for a year and prints  out whether it is a leap year or not.
    NB: These questions were taken from a book titled,  A Practical Introduction to Python Programming by  Brian Heinold.
Thanks for reading. If you have any questions, feel free to ask in the comments section, I'll gladly help. Check out my programming tutorials on YouTube and don't forget to subscribe, comment, like, and share on both YouTube and on this Blog. Lets code together!!!

181 Like

Drop your comment

© Copyright 2023 Jeremiah Taguta. All Rights Reserved.

Product of ‌

Jeremiah Taguta T/A Charis Technologies