Introduction

In this exercise, we will create a simple Python application that performs some calculations and involves simple conditional statements. For some reason (or to make things a little simpler), we will use Microsoft Visual Studio, which is freely available, but if you want, you can use any tool you judge necessary. In fact, you can create the following application directly on the console or terminal.

ApplicationPractical Learning: Creating the Application

  1. Start Microsoft Visual Studio
  2. In the Visual Studio 2022 dialog box, click Create a New Project
  3. In the Create a New Project dialog box, in the Languages combo box, select Python
  4. In the list of projects templates, click Python Application (it should be selected already).
    Click Next
  5. Specify the Project Name as PayrollEvaluation1
  6. Click Create
  7. Change the code as follows:
    print("Payroll Evaluation")
    print("==================================================")
    print("Enter the following pieces of information")
    print("--------------------------------------------------")
    print("Employee Information")
    firstName        = input("First Name:     ")
    lastName         = input("Last Name:      ")
    hSalary          = float(input("Hourly Salary:  "))
    print("==================================================")
    print("Time worked")
    print("--------------------------------------------------")
    monday       = float(input("Monday:         "))
    tuesday      = float(input("Tuesday:        "))
    wednesday    = float(input("Wednesday:      "))
    thursday     = float(input("Thursday:       "))
    friday       = float(input("Friday:         "))
    
    timeWorked   = monday + tuesday + wednesday + thursday + friday
        
    regTime      = timeWorked
    overTime     = 0.00
    regPay       = hSalary * timeWorked
    overPay      = 0.00
    
    if timeWorked > 40.00:
        regTime  = 40.00
        overTime = timeWorked - 40.00
        regPay   = hSalary * 40.00
        overPay  = hSalary * 1.50  * overTime
    
    netPay       = regPay + overPay
    
    print("==================================================")
    print("Payroll Evaluation")
    print("==================================================")
    print("Employee Information")
    print("--------------------------------------------------")
    print(f"Full Name:      {firstName} {lastName}")
    print(f"Hourly Salary:  {hSalary:5.2f}")
    print("==================================================")
    print("Time Worked Summary")
    print('--------+---------+-----------+----------+--------')
    print(" Monday | Tuesday | Wednesday | Thursday | Friday")
    print('--------+---------+-----------+----------+--------')
    print(f"{monday:6.2f}  |{tuesday:6.2f}   |{wednesday:7.2f}    |{thursday:7.2f}   |{friday:6.2f}")
    print("========+=========+===========+==========+========")
    print("\t\t\t\tPay Summary")
    print("--------------------------------------------------")
    print("\t\t\t\tTime      Pay")
    print("--------------------------------------------------")
    print(f"\t\tRegular:\t{regTime:4.2f}\t{regPay:8.2f}")
    print("--------------------------------------------------")
    print(f"\t\tOver Time:\t{overTime:5.2f}\t{overPay:8.2f}")
    print(f"-------------------------------------------------")
    print(f"\t\tNet Pay:\t{netPay:16.2f}")
    print("==================================================")
  8. To execute the project, on the main menu, click Debug and click Start Without Debugging
  9. When requested, type the values like in the first example of the previous section and press Enter each time:
    Payroll Evaluation
    ==================================================
    Enter the following pieces of information
    --------------------------------------------------
    Employee Information
    First Name:     Michael
    Last Name:      Carlock
    Hourly Salary:  28.46
    ==================================================
    Time worked
    --------------------------------------------------
    Monday:         7
    Tuesday:        8
    Wednesday:      6.5
    Thursday:       8.5
    Friday:         7.5
    ==================================================
    Payroll Evaluation
    ==================================================
    Employee Information
    --------------------------------------------------
    Full Name:      Michael Carlock
    Hourly Salary:  28.46
    ==================================================
    Time Worked Summary
    --------+---------+-----------+----------+--------
     Monday | Tuesday | Wednesday | Thursday | Friday
    --------+---------+-----------+----------+--------
      7.00  |  8.00   |   6.50    |   8.50   | 7.50
    ========+=========+===========+==========+========
                                    Pay Summary
    --------------------------------------------------
                                    Time      Pay
    --------------------------------------------------
                    Regular:        37.50    1067.25
    --------------------------------------------------
                    Over Time:       0.00       0.00
    -------------------------------------------------
                    Net Pay:                 1067.25
    ==================================================
    Press any key to continue . . .
  10. Press R to close the window and return to your programming environment
  11. To execute again, press Ctrl + F5
  12. Type the values as in the second example of the previous section and press Enter after each value:
    Payroll Evaluation
    ==================================================
    Enter the following pieces of information
    --------------------------------------------------
    Employee Information
    First Name:     Catherine
    Last Name:      Busbey
    Hourly Salary:  24.37
    ==================================================
    Time worked
    --------------------------------------------------
    Monday:         9.5
    Tuesday:        8
    Wednesday:      10.5
    Thursday:       9
    Friday:         10.5
    ==================================================
    Payroll Evaluation
    ==================================================
    Employee Information
    --------------------------------------------------
    Full Name:      Catherine Busbey
    Hourly Salary:  24.37
    ==================================================
    Time Worked Summary
    --------+---------+-----------+----------+--------
     Monday | Tuesday | Wednesday | Thursday | Friday
    --------+---------+-----------+----------+--------
      9.50  |  8.00   |  10.50    |   9.00   | 10.50
    ========+=========+===========+==========+========
                                    Pay Summary
    --------------------------------------------------
                                    Time      Pay
    --------------------------------------------------
                    Regular:        40.00     974.80
    --------------------------------------------------
                    Over Time:       7.50     274.16
    -------------------------------------------------
                    Net Pay:                 1248.96
    ==================================================
    Press any key to continue . . .
  13. Press D to close the window and return to your programming environment

Using an Opposite Conditional Statement

In the previous code, we used the Greater Than (>) operator. As it happens, its opposite if the Less Than Or Equal (<=) operator. We are going to test it.

Practical LearningPractical Learning: Using a Different Conditional Statement

  1. Change the document as follows:
    print("Payroll Evaluation")
    print("==================================================")
    print("Enter the following pieces of information")
    print("--------------------------------------------------")
    print("Employee Information")
    firstName        = input("First Name:     ")
    lastName         = input("Last Name:      ")
    hSalary          = float(input("Hourly Salary:  "))
    print("==================================================")
    print("Time worked")
    print("--------------------------------------------------")
    monday       = float(input("Monday:         "))
    tuesday      = float(input("Tuesday:        "))
    wednesday    = float(input("Wednesday:      "))
    thursday     = float(input("Thursday:       "))
    friday       = float(input("Friday:         "))
    
    timeWorked   = monday + tuesday + wednesday + thursday + friday
        
    regTime      = 40.00
    overTime     = timeWorked - 40.00
    regPay       = hSalary * 40.00
    overPay      = hSalary * 1.50  * overTime
    
    if timeWorked <= 40.00:
        regTime  = timeWorked
        overTime = 0.00
        regPay   = hSalary * timeWorked
        overPay  = 0.00
    
    netPay       = regPay + overPay
    
    print("==================================================")
    print("Payroll Evaluation")
    print("==================================================")
    print("Employee Information")
    print("--------------------------------------------------")
    print(f"Full Name:      {firstName} {lastName}")
    print(f"Hourly Salary:  {hSalary:5.2f}")
    print("==================================================")
    print("Time Worked Summary")
    print('--------+---------+-----------+----------+--------')
    print(" Monday | Tuesday | Wednesday | Thursday | Friday")
    print('--------+---------+-----------+----------+--------')
    print(f"{monday:6.2f}  |{tuesday:6.2f}   |{wednesday:7.2f}    |{thursday:7.2f}   |{friday:6.2f}")
    print("========+=========+===========+==========+========")
    print("\t\t\t\tPay Summary")
    print("--------------------------------------------------")
    print("\t\t\t\tTime      Pay")
    print("--------------------------------------------------")
    print(f"\t\tRegular:\t{regTime:4.2f}\t{regPay:8.2f}")
    print("--------------------------------------------------")
    print(f"\t\tOver Time:\t{overTime:5.2f}\t{overPay:8.2f}")
    print(f"-------------------------------------------------")
    print(f"\t\tNet Pay:\t{netPay:16.2f}")
    print("==================================================")
  2. To execute, press Ctrl + F5
  3. Type the values as in the second example of the previous section and press Enter after each value:
    Payroll Evaluation
    ==================================================
    Enter the following pieces of information
    --------------------------------------------------
    Employee Information
    First Name:     Herbert
    Last Name:      Mann
    Hourly Salary:  32.17
    ==================================================
    Time worked
    --------------------------------------------------
    Monday:         7.5
    Tuesday:        8.
    Wednesday:      8.5
    Thursday:       6.5
    Friday:         7
    ==================================================
    Payroll Evaluation
    ==================================================
    Employee Information
    --------------------------------------------------
    Full Name:      Herbert Mann
    Hourly Salary:  32.17
    ==================================================
    Time Worked Summary
    --------+---------+-----------+----------+--------
     Monday | Tuesday | Wednesday | Thursday | Friday
    --------+---------+-----------+----------+--------
      7.50  |  8.00   |   8.50    |   6.50   |  7.00
    ========+=========+===========+==========+========
                                    Pay Summary
    --------------------------------------------------
                                    Time      Pay
    --------------------------------------------------
                    Regular:        37.50    1206.38
    --------------------------------------------------
                    Over Time:       0.00       0.00
    -------------------------------------------------
                    Net Pay:                 1206.38
    ==================================================
    Press any key to continue . . .
  4. Press any key to close the window and return to your programming environment
  5. When requested, type the values and press Enter each time:
    Payroll Evaluation
    ==================================================
    Enter the following pieces of information
    --------------------------------------------------
    Employee Information
    First Name:     Jeanine
    Last Name:      Hewsen
    Hourly Salary:  34.05
    ==================================================
    Time worked
    --------------------------------------------------
    Monday:         9
    Tuesday:        8.5
    Wednesday:      10.5
    Thursday:       9.5
    Friday:         10
    ==================================================
    Payroll Evaluation
    ==================================================
    Employee Information
    --------------------------------------------------
    Full Name:      Jeanine Hewsen
    Hourly Salary:  34.05
    ==================================================
    Time Worked Summary
    --------+---------+-----------+----------+--------
     Monday | Tuesday | Wednesday | Thursday | Friday
    --------+---------+-----------+----------+--------
      9.00  |  8.50   |  10.50    |   9.50   | 10.00
    ========+=========+===========+==========+========
                                    Pay Summary
    --------------------------------------------------
                                    Time      Pay
    --------------------------------------------------
                    Regular:        40.00    1362.00
    --------------------------------------------------
                    Over Time:       7.50     383.06
    -------------------------------------------------
                    Net Pay:                 1745.06
    ==================================================
    Press any key to continue . . .
  6. Press any key to close the DOS window and return to your programming environment
  7. Close your programming environment

Home Copyright © 2021-2025, FunctionX Thursday 12 December 2024, 14:11 Home