Conditional Conjunctions

Introduction

In the previous lesson, we learned how to use some keywords (if, elif, and else). The next step to manage the various situations of a program is to combine conditions. You have various options.

ApplicationPractical Learning: Introducing Else Conditions

  1. Start Microsoft Visual Studio
  2. Create a new Python Application named TaxEvaluation2
  3. In the empty document, type the following lines:
    print("============================================")
    print("\t - Georgia - State Income Tax -")
    print("============================================")
    
    filing_status : str
    added_amount  : float
    tax_rate      : float
    gross_salary  : float
    
    print("Enter the information for tax preparation")
    gross_salary : float = float(input("Gross Salary:        "))
    
    # Georgia
    filing_status = 'Married Filing Joint or Head of Household'
    
    if gross_salary >= 10_000:
        tax_rate = 5.75
        added_amount = 340
    elif gross_salary >= 7_000:
        tax_rate = 5.00
        added_amount = 190
    elif gross_salary >= 5_000:
        tax_rate = 4.00
        added_amount = 110
    elif gross_salary >= 3_000:
        tax_rate = 3.00
        added_amount = 50
    elif gross_salary >= 1_000:
        tax_rate = 2.00
        added_amount = 10
    else: # if gross_salary < 1_000:
        tax_rate = 1.00
        added_amount = 0
    
    tax_amount = added_amount + (gross_salary * tax_rate / 100.00)
    net_pay = gross_salary - tax_amount
    
    print("==============================================")
    print("\t- Georgia - State Income Tax -")
    print("----------------------------------------------")
    print(f"Gross Salary:  {gross_salary:.2f}")
    print(f"Filing Status: {filing_status:s}")
    print(f"Tax Rate:      {tax_rate:.2f}%")
    print(f"Tax Amount:    {tax_amount:.2f}")
    print(f"Net Pay:       {net_pay:.2f}")
    print("==============================================")
  4. To execute the application and test it, on the main menu, click Run -> Run Without Debugging
  5. In the Terminal, click on the right side of Gross Salary, type 999.99 and press Enter
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        999.99
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  999.99
    Filing Status: Married Filing Joint or Head of Household
    Tax Rate:      1.00%
    Tax Amount:    10.00
    Net Pay:       989.99
    ==============================================
  6. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  7. In the Terminal, click on the right side of Gross Salary, type 1000.00 and press Enter
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        1000.00
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  1000.00
    Filing Status: Married Filing Joint or Head of Household
    Tax Rate:      2.00%
    Tax Amount:    30.00
    Net Pay:       970.00
    ==============================================
  8. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  9. In the Terminal, click on the right side of Gross Salary, type 5726.87 and press Enter
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        5726.87
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  5726.87
    Filing Status: Married Filing Joint or Head of Household
    Tax Rate:      4.00%
    Tax Amount:    339.07
    Net Pay:       5387.80
    ==============================================
  10. Change the document as follows:
    print("============================================")
    print("\t - Georgia - State Income Tax -")
    print("============================================")
    
    filing_status : str
    added_amount  : float
    tax_rate      : float
    gross_salary  : float
    
    print("Enter the information for tax preparation")
    gross_salary : float = float(input("Gross Salary:        "))
    
    # Georgia
    filing_status = 'Single'
    
    if gross_salary >= 7_000:
        tax_rate = 5.75
        added_amount = 230
    elif gross_salary >= 5_250:
        tax_rate = 5.00
        added_amount = 143
    elif gross_salary >= 3_750:
        tax_rate = 4.00
        added_amount = 83
    elif gross_salary >= 2_250:
        tax_rate = 3.00
        added_amount = 38
    elif gross_salary >= 750:
        tax_rate = 2.00
        added_amount = 8
    else: # if gross_salary < 750:
        tax_rate = 1.00
        added_amount = 0
    
    tax_amount = added_amount + (gross_salary * tax_rate / 100.00)
    net_pay = gross_salary - tax_amount
    
    print("==============================================")
    print("\t- Georgia - State Income Tax -")
    print("----------------------------------------------")
    print(f"Gross Salary:  {gross_salary:.2f}")
    print(f"Filing Status: {filing_status:s}")
    print(f"Tax Rate:      {tax_rate:.2f}%")
    print(f"Tax Amount:    {tax_amount:.2f}")
    print(f"Net Pay:       {net_pay:.2f}")
    print("==============================================")
  11. To execute the application and test it, on the main menu, click Run -> Run Without Debugging
  12. In the Terminal, click on the right side of Gross Salary, type 149.99 and press Enter
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        749.99
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  749.99
    Filing Status: Single
    Tax Rate:      1.00%
    Tax Amount:    7.50
    Net Pay:       742.49
    ==============================================
  13. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  14. In the Terminal, click on the right side of Gross Salary, type 1000 and press Enter
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        150.00
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  150.00
    Filing Status: Single
    Tax Rate:      1.00%
    Tax Amount:    1.50
    Net Pay:       148.50
    ==============================================
  15. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  16. In the Terminal, click on the right side of Gross Salary, type 5726.87 and press Enter
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        5726.87
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  5726.87
    Filing Status: Single
    Tax Rate:      5.00%
    Tax Amount:    429.34
    Net Pay:       5297.53
    ==============================================

Nesting a Conditional Statement

You can create a conditional statement in the body of another conditional statement. This is referred to as nesting the condition. The condition nesting can be formulated as follows:

if condition1:     # The nesting, main, parent, first, or external condition
    if condition2: # The nested, child, second, or internal condition
    	statement(s)

The first condition1 will be checked. If it evaluates to True, then the nested condition, condition2, will execute. If the first condition1 evaluates to False, the nested condition will not be checked. Here is an example:

print("Make the following selections to evaluate your employment benefits")
work_status : str = input("Work Status (e=Employee, c=Contractor):       ")
empl_status : str = input("Employment Status (f=Full-Time, p=Part-Time): ")

conclusion = 'Unknown'

if work_status == 'e':
    if empl_status == 'f':
        conclusion = "The employee qualifies for all benefits"

print("------------------------------------------------------------------")
print("Decision: ", conclusion)
print("==================================================================")

Here is an example of running the program:

Make the following selections to evaluate your employment benefits
Work Status (e=Employee, c=Contractor):       e
Employment Status (f=Full-Time, p=Part-Time): p
------------------------------------------------------------------
Decision:  Unknown
==================================================================
Press any key to continue . . .

Here is another example of running the program:

Make the following selections to evaluate your employment benefits
Work Status (e=Employee, c=Contractor):       e
Employment Status (f=Full-Time, p=Part-Time): f
------------------------------------------------------------------
Decision:  The employee qualifies for all benefits
==================================================================
Press any key to continue . . .

In the above example, the nested condition contains a simple if statement. In reality, a nested condition can be as expanded as you want by having as many statements as necessary.

You can nest a conditional statement in another conditional statement, then nest that new conditional statement in another conditional statement, and so on. This can be formulated as follows:

if condition1:
    if condition2:
        if condition3:
            statement(s)

Practical LearningPractical Learning: Nesting a Conditional Statement

  1. To nest some conditional statements, change the document as follows:
    print("============================================")
    print("\t - Georgia - State Income Tax -")
    print("============================================")
    
    filing_status : str
    added_amount  : float
    tax_rate      : float = 0.00
    gross_salary  : float = 0.00
    
    print("Enter the information for tax preparation")
    gross_salary : float = float(input("Gross Salary:        "))
    print("Filing Status")
    print("s - Single")
    print("c - Married Filing Joint or Head of Household")
    answer : str = input("Enter Filing Status: ")
    
    if answer == 's':
        filing_status = 'Single'
    
        if gross_salary >= 7_000:
            tax_rate = 5.75
            added_amount = 230
        elif gross_salary >= 5_250:
            added_amount = 143
            tax_rate = 5.00
        elif gross_salary >= 3_750:
            added_amount = 83
            tax_rate = 4.00
        elif gross_salary >= 2_250:
            added_amount = 38
            tax_rate = 3.00
        elif gross_salary >= 750:
            added_amount = 8
            tax_rate = 2.00
        else: # if gross_salary < 750:
            added_amount = 0
            tax_rate = 1.00
    else:
        filing_status = 'Married Filing Joint or Head of Household'
    
        if gross_salary >= 10_000:
            added_amount = 340
            tax_rate = 5.75
        elif gross_salary >= 7_000:
            added_amount = 190
            tax_rate = 5.00
        elif gross_salary >= 5_000:
            added_amount = 110
            tax_rate = 4.00
        elif gross_salary >= 3_000:
            added_amount = 50
            tax_rate = 3.00
        elif gross_salary >= 1_000:
            added_amount = 10
            tax_rate = 2.00
        else: # if gross_salary < 1_000:
            added_amount = 0
            tax_rate = 1.00
    
    tax_amount = added_amount + (gross_salary * tax_rate / 100.00)
    net_pay = gross_salary - tax_amount
    
    print("==============================================")
    print("\t- Georgia - State Income Tax -")
    print("----------------------------------------------")
    print(f"Gross Salary:  {gross_salary:.2f}")
    print(f"Filing Status: {filing_status:s}")
    print(f"Tax Rate:      {tax_rate:.2f}%")
    print(f"Tax Amount:    {tax_amount:.2f}")
    print(f"Net Pay:       {net_pay:.2f}")
    print("==============================================")
  2. To execute the application and test it, on the main menu, click Run -> Run Without Debugging
  3. In the Terminal, click on the right side of Gross Salary, type 5368.47 and press Enter
  4. For the Filing Status, type s and press Enter:
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        5368.47
    Filing Status
    s - Single
    c - Married Filing Joint or Head of Household
    Enter Filing Status: s
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  5368.47
    Filing Status: Single
    Tax Rate:      5.00%
    Tax Amount:    411.42
    Net Pay:       4957.05
    ==============================================
  5. To execute the application again, on the main menu, click Run -> Run Without Debugging
  6. In the Terminal, click on the right side of Gross Salary, type 5368.47 and press Enter
  7. For the Filing Status, type c and press Enter:
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        5368.47
    Filing Status
    s - Single
    c - Married Filing Joint or Head of Household
    Enter Filing Status: c
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  5368.47
    Filing Status: Married Filing Joint or Head of Household
    Tax Rate:      4.00%
    Tax Amount:    324.74
    Net Pay:       5043.73
    ==============================================
  8. To expand on nesting conditional statements, change the document as follows:
    print("============================================")
    print("\t - Georgia - State Income Tax -")
    print("============================================")
    
    filing_status : str
    added_amount  : float
    tax_rate      : float
    gross_salary  : float
    
    print("Enter the information for tax preparation")
    gross_salary : float = float(input("Gross Salary:        "))
    print("Filing Status")
    print("s - Single")
    print("f - Married Filing Separate")
    print("j - Married Filing Joint or Head of Household")
    answer : str = input("Enter Filing Status: ")
    
    # Georgia
    if answer == 's':
        filing_status = 'Single'
    
        if gross_salary >= 7_000:
            tax_rate = 5.75
            added_amount = 230
        elif gross_salary >= 5_250:
            added_amount = 143
            tax_rate = 5.00
        elif gross_salary >= 3_750:
            added_amount = 83
            tax_rate = 4.00
        elif gross_salary >= 2_250:
            added_amount = 38
            tax_rate = 3.00
        elif gross_salary >= 750:
            added_amount = 8
            tax_rate = 2.00
        else: # if gross_salary < 750:
            added_amount = 0
            tax_rate = 1.00
    elif answer == 'j':
        filing_status = 'Married Filing Joint or Head of Household'
    
        if gross_salary >= 10_000:
            added_amount = 340
            tax_rate = 5.75
        elif gross_salary >= 7_000:
            added_amount = 190
            tax_rate = 5.00
        elif gross_salary >= 5_000:
            added_amount = 110
            tax_rate = 4.00
        elif gross_salary >= 3_000:
            added_amount = 50
            tax_rate = 3.00
        elif gross_salary >= 1_000:
            added_amount = 10
            tax_rate = 2.00
        else: # if gross_salary < 1_000:
            added_amount = 0
            tax_rate = 1.00
    else: # if answer == 'f':
        filing_status = 'Married Filing Separate'
    
        if gross_salary >= 5_000:
            added_amount = 170
            tax_rate = 5.75
        elif gross_salary >= 3_500:
            added_amount = 95
            tax_rate = 5.00
        elif gross_salary >= 2_500:
            added_amount = 55
            tax_rate = 4.00
        elif gross_salary >= 1_500:
            added_amount = 25
            tax_rate = 3.00
        elif gross_salary >= 500:
            added_amount = 5
            tax_rate = 2.00
        else: # if gross_salary < 1_000:
            added_amount = 0
            tax_rate = 1.00
    
    tax_amount = added_amount + (gross_salary * tax_rate / 100.00)
    net_pay = gross_salary - tax_amount
    
    print("==============================================")
    print("\t- Georgia - State Income Tax -")
    print("----------------------------------------------")
    print(f"Gross Salary:  {gross_salary:.2f}")
    print(f"Filing Status: {filing_status:s}")
    print(f"Tax Rate:      {tax_rate:.2f}%")
    print(f"Tax Amount:    {tax_amount:.2f}")
    print(f"Net Pay:       {net_pay:.2f}")
    print("==============================================")
  9. To execute the application, on the main menu, click Run -> Run Without Debugging
  10. In the Terminal, click on the right side of Gross Salary, type 5278.75 and press Enter
  11. For the Filing Status, type s and press Enter:
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        5278.75
    Filing Status
    s - Single
    f - Married Filing Separate
    j - Married Filing Joint or Head of Household
    Enter Filing Status: s
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  5278.75
    Filing Status: Single
    Tax Rate:      5.00%
    Tax Amount:    406.94
    Net Pay:       4871.81
    ==============================================
  12. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  13. In the Terminal, click on the right side of Gross Salary, type 5278.75 and press Enter
  14. For the Filing Status, type j and press Enter
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        5278.75
    Filing Status
    s - Single
    f - Married Filing Separate
    j - Married Filing Joint or Head of Household
    Enter Filing Status: j
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  5278.75
    Filing Status: Married Filing Joint or Head of Household
    Tax Rate:      4.00%
    Tax Amount:    321.15
    Net Pay:       4957.60
    ==============================================
  15. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  16. In the Terminal, click on the right side of Gross Salary, type 5278.75 and press Enter
  17. For the Filing Status, type Married but filing separately and press Enter
    ============================================
             - Georgia - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        5278.75
    Filing Status
    s - Single
    f - Married Filing Separate
    j - Married Filing Joint or Head of Household
    Enter Filing Status: Married but filing separately
    ==============================================
            - Georgia - State Income Tax -
    ----------------------------------------------
    Gross Salary:  5278.75
    Filing Status: Married Filing Separate
    Tax Rate:      5.75%
    Tax Amount:    473.53
    Net Pay:       4805.22
    ==============================================

Boolean Conjunctions

Remember that you can nest one condition in another condition as in:

if condition1:
    if condition2:
        statement(s)

When you nest a condition, you are in fact indicating that "if condition1 verifies, then if condition2 verifies, do this...". The external condition must be verified first as being True or False (depending on how you wrote the conditional statement). If that first (the external) condition is True, the second (the internal) condition is checked. If the first (or external) condition is not valid, the second (the internal) condition is not evaluated. To support a simplified technique to apply this description, you can use an operator referred to as "and". Its primary formula is:

condition1 and condition2

You must formulate each condition to produce a True or a False result. The result is as follows:

The conjunction operation can be resumed as follows:

Condition 1 Contition 2 Condition 1 and Condition 2
False False False
False True False
True False False
True True True

Practical LearningPractical Learning: Creating a Boolean Conjunction

  1. Change the document as follows:
    print("============================================")
    print("\t - Arkansas - State Income Tax -")
    print("============================================")
    
    added_amount  : float
    tax_rate      : float
    gross_salary  : float
    
    print("Enter the information for tax preparation")
    gross_salary : float = float(input("Gross Salary:        "))
    
    # Arkansas
    if (gross_salary >= 0.00) and (gross_salary <= 4_000):
        tax_rate = 2.00
    elif (gross_salary > 4_000) and (gross_salary <= 8_000):
        tax_rate = 4.00
    elif (gross_salary > 8_000) and (gross_salary <= 79_300):
        tax_rate = 5.90
    else: # if gross_salary > 79_300:
        tax_rate = 6.60
    
    tax_amount = gross_salary * tax_rate / 100.00
    net_pay    = gross_salary - tax_amount
    
    print("==============================================")
    print("\t- Arkansas - State Income Tax -")
    print("----------------------------------------------")
    print(f"Gross Salary:  {gross_salary:.2f}")
    print(f"Tax Rate:      {tax_rate:.2f}%")
    print(f"Tax Amount:    {tax_amount:.2f}")
    print(f"Net Pay:       {net_pay:.2f}")
    print("==============================================")
  2. To execute the application, on the main menu, click Run -> Run Without Debugging
  3. In the Terminal, click on the right side of Gross Salary, type 8000.00 and press Enter:
    ============================================
             - Arkansas - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        8000.00
    ==============================================
            - Arkansas - State Income Tax -
    ----------------------------------------------
    Gross Salary:  8000.00
    Tax Rate:      4.00%
    Tax Amount:    320.00
    Net Pay:       7680.00
    ==============================================
  4. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  5. In the Terminal, click on the right side of Gross Salary, type 8000.01 and press Enter:
    ============================================
             - Arkansas - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:        8000.01
    ==============================================
            - Arkansas - State Income Tax -
    ----------------------------------------------
    Gross Salary:  8000.01
    Tax Rate:      5.90%
    Tax Amount:    472.00
    Net Pay:       7528.01
    ==============================================
  6. Change the document as follows:
    print("============================================")
    print("\t - State Income Tax -")
    print("============================================")
    
    gross_salary  : float
    tax_rate      : float = 4.95
    
    print("Enter the information for tax preparation")
    gross_salary : float = float(input("Gross Salary:   "))
    
    # Illinois
    tax_amount = gross_salary * tax_rate / 100.00
    net_pay    = gross_salary - tax_amount
    
    print("==============================================")
    print("\t- Illinois - State Income Tax -")
    print("----------------------------------------------")
    print(f"Gross Salary:  {gross_salary:.2f}")
    print(f"Tax Rate:      {tax_rate:.2f}%")
    print(f"Tax Amount:    {tax_amount:.2f}")
    print(f"Net Pay:       {net_pay:.2f}")
    print("==============================================")
  7. To execute the application, on the main menu, click Run -> Run Without Debugging
  8. In the Terminal, click on the right side of Gross Salary, type 968.68 and press Enter:
    ============================================
             - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:   968.68
    ==============================================
            - Illinois - State Income Tax -
    ----------------------------------------------
    Gross Salary:  968.68
    Tax Rate:      4.95%
    Tax Amount:    47.95
    Net Pay:       920.73
    ==============================================
  9. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  10. In the Terminal, click on the right side of Gross Salary, type 2427.96 and press Enter:
    ============================================
             - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:   2427.96
    ==============================================
            - Illinois - State Income Tax -
    ----------------------------------------------
    Gross Salary:  2427.96
    Tax Rate:      4.95%
    Tax Amount:    120.18
    Net Pay:       2307.78
    ==============================================

Combining Various Conjunctions

Depending on your goal, if two conditions are not enough, you can create as many conjunctions as you want. The formula to follow is:

condition1 and condition2 and condition3 and . . . and condition_n

When the expression is checked, if any of the operations is False, the whole operation is False. The only time the whole operation is True is if all of the operations are True.

Of course, you can nest a Boolean condition inside another conditional statement.

Boolean Disjunctions

Introduction

A Boolean disjunction is a conditional statement where you combine more than one condition but only one of the conditions needs to be True for the whole operation to be True. This operation is performed using a Boolean operator named "or". The primary formula to follow is:

condition1 or condition2

The operation works as follows:

The operation can be resumed as follows:

Condition 1 Contition 2 Condition 1 and Condition 2 Condition 1 or Condition 2
False False False False
False True False True
True False False True
True True True True

Practical LearningPractical Learning: Creating a Disjunction

  1. Change the document as follows:
    print("============================================")
    print("\t - State Income Tax -")
    print("============================================")
    
    gross_salary  : float
    state         : str   = "OO"
    tax_rate      : float = 0.00
    
    print("Enter the information for tax preparation")
    gross_salary : float = float(input("Gross Salary:          "))
    print("List of States")
    print("AR - Arkansas")
    print("GA - Georgia")
    print("IL - Illinois")
    print("MS - Mississippi")
    print("UT - Utah")
    state = input("Enter State Letters: ")
    
    # Illinois OR Utah
    if (state == "IL") or (state == "UT"):
        tax_rate = 4.95
    
        tax_amount = gross_salary * tax_rate / 100.00
        net_pay    = gross_salary - tax_amount
    
        print("==============================================")
        print("\t- Illinois/Utah - State Income Tax -")
        print("----------------------------------------------")
        print(f"Gross Salary:  {gross_salary:.2f}")
        print(f"Tax Rate:      {tax_rate:.2f}%")
        print(f"Tax Amount:    {tax_amount:.2f}")
        print(f"Net Pay:       {net_pay:.2f}")
    
    print("==============================================")
  2. To execute the application, on the main menu, click Run -> Run Without Debugging
  3. In the Terminal, click on the right side of Gross Salary, type 968.68 and press Enter
  4. When the state is requested, type UT and press Enter
    ============================================
             - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:          968.68
    List of States
    AR - Arkansas
    GA - Georgia
    IL - Illinois
    MS - Mississippi
    UT - Utah
    Enter State Letters: UT
    ==============================================
            - Illinois/Utah - State Income Tax -
    ----------------------------------------------
    Gross Salary:  968.68
    Tax Rate:      4.95%
    Tax Amount:    47.95
    Net Pay:       920.73
    ==============================================
  5. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  6. In the Terminal, click on the right side of Gross Salary, type 968.68 and press Enter
  7. When the state is requested, type Alabama and press Enter
    ============================================
             - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:          968.68
    List of States
    AR - Arkansas
    GA - Georgia
    IL - Illinois
    MS - Mississippi
    UT - Utah
    Enter State Letters: Alabama
    ==============================================

Combining Various Disjunctions

You can create a conditional statement that includes as many disjunctions as you want. The formula to follow is:

condition1 or condition2 or . . . or condition_n

The rule is the same: If any one of the individual operations is True, the whole operation is True. The whole operation is False only if all of the operations are False. Here are two examples:

print("============================================")
print("\t - State Income Tax -")
print("============================================")

gross_salary  : float
state         : str   = "OO"
tax_rate      : float = 0.00

print("Enter the information for tax preparation")
gross_salary : float = float(input("Gross Salary:          "))
print("List of States")
print("AK - Alaska")
print("AR - Arkansas")
print("GA - Georgia")
print("IL - Illinois")
print("KY - Kentucky")
print("MA - Massachusetts")
print("MS - Mississippi")
print("NH - New Hampshire")
print("NV - Nevada")
print("SD - South Dakota")
print("TX - Texas")
print("UT - Utah")
print("WA - Washington")
print("WY - Wyoming")
state = input("Enter State Letters: ")

# Illinois OR Utah
if (state == "IL") or (state == "UT"):
    tax_rate = 4.95
elif (state == "KY") or (state == "MA") or (state == "NH"):
    tax_rate = 5.00
elif (state == "AK") or (state == "NV") or (state == "SD") or (state == "TX") or (state == "WA") or (state == "WY"):
    tax_rate = 0.00

tax_amount = gross_salary * tax_rate / 100.00
net_pay    = gross_salary - tax_amount

print("==============================================")
print("\t - State Income Tax -")
print("----------------------------------------------")
print(f"Gross Salary:  {gross_salary:.2f}")
print(f"Tax Rate:      {tax_rate:.2f}%")
print(f"Tax Amount:    {tax_amount:.2f}")
print(f"Net Pay:       {net_pay:.2f}")

print("==============================================")

Practical LearningPractical Learning: Creating a Disjunction

  1. Change the document as follows:
    print("============================================")
    print("\t - State Income Tax -")
    print("============================================")
    
    gross_salary  : float
    state         : str   = "OO"
    tax_rate      : float = 0.00
    
    print("Enter the information for tax preparation")
    gross_salary : float = float(input("Gross Salary:          "))
    print("List of States")
    print("AR - Arkansas")
    print("GA - Georgia")
    print("IL - Illinois")
    print("KY - Kentucky")
    print("MA - Massachusetts")
    print("MS - Mississippi")
    print("NH - New Hampshire")
    print("UT - Utah")
    state = input("Enter State Letters: ")
    
    # Illinois OR Utah
    if (state == "IL") or (state == "UT"):
        tax_rate = 4.95
    elif (state == "KY") or (state == "MA") or (state == "NH"):
        tax_rate = 5.00
    
    tax_amount = gross_salary * tax_rate / 100.00
    net_pay    = gross_salary - tax_amount
    
    print("==============================================")
    print("\t - State Income Tax -")
    print("----------------------------------------------")
    print(f"Gross Salary:  {gross_salary:.2f}")
    print(f"Tax Rate:      {tax_rate:.2f}%")
    print(f"Tax Amount:    {tax_amount:.2f}")
    print(f"Net Pay:       {net_pay:.2f}")
    
    print("==============================================")
  2. To execute the application, on the main menu, click Run -> Run Without Debugging
  3. In the Terminal, click on the right side of Gross Salary, type 968.68 and press Enter
  4. When the state is requested, type KY and press Enter
    ============================================
             - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary:          968.68
    List of States
    AR - Arkansas
    GA - Georgia
    IL - Illinois
    MS - Mississippi
    UT - Utah
    Enter State Letters: UT
    ==============================================
            - Illinois/Utah - State Income Tax -
    ----------------------------------------------
    Gross Salary:  968.68
    Tax Rate:      4.95%
    Tax Amount:    47.95
    Net Pay:       920.73
    ==============================================
  5. To execute the application and test it again, on the main menu, click Run -> Run Without Debugging
  6. In the Terminal, click on the right side of Gross Salary, type 3168.97 and press Enter
  7. When the state is requested, type MA and press Enter
    ============================================
             - State Income Tax -
    ============================================
    Enter the information for tax preparation   
    Gross Salary:          3168.97
    List of States
    AR - Arkansas
    GA - Georgia
    IL - Illinois
    KY - Kentucky
    MA - Massachusetts
    MS - Mississippi
    NH - New Hampshire
    UT - Utah
    Enter State Letters: MA
    ==============================================
             - State Income Tax -
    ----------------------------------------------
    Gross Salary:  3168.97
    Tax Rate:      5.00%
    Tax Amount:    158.45
    Net Pay:       3010.52
    ==============================================

Combining Conjunctions and Disjunctions

Conjunctions and disjunctions can be used in the same expression. A conjunction (or disjunction) can be used to evaluate one sub-expression while a disjunction (or conjunction) can be used to evaluate another sub-expression.

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2021-2022, FunctionX Thursday 30 December 2021 Next