ENGG1003 - Lab09 - PythonDataProcessing 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

ENGG1003

Digital Literacy and


Computational Thinking – P

Lab 09 Python Data


Processing
2023-24 Term 1
Setup Python on Your Computer
(Recap)
• Download and follow the steps on Blackboard
• Lab 01 -
InstallationGuide_python_Win11_macOS.pptx
• Windows users: slides 1 – 6
• macOS users: slides 7 – 15

• You may also work on your VM !


Save Your Work Properly
(Recap)
• Setup your own filing system: create a folder for
your works in ENGG1003
• Keep on your own computer, e.g., Documents
• Loading…
Keep on portable storage such as USB drive
• Keep on cloud storage such as OneDrive (CUHK
O365)

• Folder structure is hierarchical, i.e., tree-like


with branches called sub-folders
Lab 09 Activities

➢ Use of basic python commands


➢ Input() and data types
➢ Defining and calling functions
➢ Solving problems using if: elif: else:
➢ Bonus Task
Academic Honesty
➢ You must read and observe the University Guideline on
Academic Honesty
(https://www.cuhk.edu.hk/policy/academichonesty/)
➢ You should NOT share your file to others, regardless of your
intention
➢ You should NOT obtain other’s works by any means
➢ Loading…
Submitting the wrong file by “accident” will NOT be
accepted as an excuse – so please double-check which file
you have submitted
Important : Please read and observe the declaration statement when you upload and submit your work on Blackboard.

You that
areand
➢Iacknowledged,
declare welcome
the seeking
lab work here help
submitted is at IT Clinic
original except (HCA 328)
for source during
material explicitly
that the same or closely related material has not been previously submitted for
office hours.
another course.

I also acknowledge that I am aware of University policy and regulations on honesty in academic
work, and of the disciplinary guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the website.

University Guideline on Academic Honesty: https://www.cuhk.edu.hk/policy/academichonesty/


Mandatory Tasks
Lab Tasks Overview
➢ In this lab, we will work on CUHK supermarket application
problem and practice python functions.
➢ Understanding the application problem will help you completing your
tasks
➢ In this application there are 3 compulsory tasks and 1 bonus task.
➢ Task 1: Complete calcWeeklyWage() function and call the function
➢ Task 2: Complete calcWithOverTimeWage() function and call the
function
➢ Task 3: Complete payScale() function and call the function
➢ Bonus Task 4: Complete roleWage() function and call the function
Ø To start, please download the provided python file from
BlackBoard.
Ø Submission: Submit the final version (one file only) containing
your work for all Tasks, no matter you attempt Bonus Task 4 or
not.
Ø Save all your works from time-to-time to avoid any missing.
Task 1
Task 1 Scenario


Python function recap

➢ A python function works in a following way:

Anything that starts with # is a comment which is not executed

# define a function named sayHi

def sayHi():
Define the function first
print("Hi!")

# call/ use the function


Hi!
sayHi() Call the function to check the result

➢ For more details, refer to the lecture slides.


Task 1

➢ In task 1, we shall complete


and call a function calcWeeklyWage() Define the function first
calcWeeklyWage()

Loading…
This function takes TWO
arguments and multiply them
to calculate weekly wage.
Pass the
parameters

➢ The function shall return a


value as result. #code to call the function
Complete the code to
➢ After completing the - Request parameters
call the function
- Call the function
function, add some code to
call the calcWeeklyWage() Print and check the result
function.
➢ Locate a given comment line
➢ # Call the function for TASK
1
Task 1
➢ To start, download the “employeeWage.py” file from Blackboard.

➢ Launch IDLE Python, click File Open browse and pick


employeeWage.py and click open.

➢ Read comments by TASKs/TODO/parts: def/Input/Call/Output

➢ Complete the “calcWeeklyWages( )” function to calculate the


weekly wages for all employees of the supermarket.

➢ Add some code to get some user input, test and check the result

➢ We shall use input("...") statement to ask for total hours of work that is
a float type value.

➢ Call the “calcWeeklyWages()” function

➢ For example, an employee worked for 39.5 hours/week and assume


Task 1: Hints

def calcWeeklyWage(totalHours, hourlyWage):

total_Weekly_Wage = # Apply the formula listed on slide 9

return total_Weekly_Wage

# Assume # Firstly TASK 1 adopts a FIXED hourlyWage $55


It will be changed in TASKS 3 and 4!
hourlyWage = 55
# Input # Use the input( ) command with a message "Enter the number of
totalHours = hours worked by an employee : ", to request a float from the user
Be careful about spaces, and the data type conversion!

# Call the function for TASK 1


total_Weekly_Wage = Type here to call the calcWeeklyWage( ____ , ____ ) function

# Output
print(f'Wages for {totalHours} hours at ${hourlyWage} per hour
are ${total_Weekly_Wage}')
Task 1 – Check your output
Inputs Outputs
hourlyWage FIXED in TASK 1 totalHours total_Weekly_Wage
(will change in TASKS 3 and 4) calcWeeklyWage( )
55 40 $2200.0
55 30 $1650.0
55 28.5 $1567.5

➢ Completion Reminder:
➢ After completing task 1, save your work in “employeeWage.py”
➢ Make sure the “employeeWage.py” file has complete code of task 1.
Task 2
Task 2 Scenario
➢ Let us now assume that every employee of CUHK
supermarket work for 40 hours/week as a standard.

➢ Employees can also work overtime (OT), i.e., beyond 40


hours, the hourly wage of extra hour(s) will increase by 50%,
i.e., as 1.5 times.

➢ For example: an employee worked for 42.8 hours a week


and the employee is paid $55 per hour (same FIXED rate as in
TASK 1)

➢ Total hours worked for is 42.8 in the week

➢ For the first 40 hours in the standard, the paid is $2200.

➢ The OT is 2.8 hours, at OT paid $55 x 1.5 = $82.5


Therefore, the employee earns 2.8 x $82.5 = $231 during OT.
Task 2 Scenario calcWeeklyWage( )
Use calcWeeklyWage()
from task1

➢ In task 2, we shall complete


and call another function Pass the
parameters
calcWithOverTimeWage( ) :
➢ if an employee worked for 40
hours or less, calculate the Define the function first,
weekly wage by calling task 1 then make use of
calcWithOverTimeWage( )
calcWeeklyWage()
function calcWeeklyWage( ) function in operation

➢ else, find overtime hours and


OT hourly wage (x 1.5) Pass the
parameters

Calculate OT paid by calling


calcWeeklyWage( ) again.
#code to call the function
Complete the code to
Remember to include the - Request parameters
call the function
standard paid for the first 40 - Call the function
hours of work!
Print and check the result
➢ The function shall return a
value as result.
Task 2 Scenario

➢ To start, we will continue to work in “employeeWage.py” file.


➢ Read the comments/TODO for TASK2 in the file CAREFULLY.
➢ Complete the function calcWithOverTimeWage( ) that considers
the number of hours worked by an employee and calculates the
weekly wage accordingly.
➢ Use if-else statement in calcWithOverTimeWage( ) to check the
number of hours worked by an employee.
➢ calcWithOverTimeWage( ) can call the calcWeeklyWage() function
completed in Task 1 (code re-use.)
➢ If an employee works for 40 hours or less, calculate the weekly
wage using the standard formula by calling
calcWeeklyWage() function.
➢ If the employee has worked overtime, calculate the overtime and
then calculate the total weekly wage for an employee.
Task 2 FOLLOWS Task 1
def calcWeeklyWage(totalHours, hourlyWage):
# Completed in task 1

def calcWithOverTimeWage(totalHours, hourlyWage):


# If the totalHours is less than or equal to 40 then calculate the
totalWage with the hours; by calling calcWeeklyWage() function

# else Refer to slide 16


calculate overtime period
calculate (totalWage for 40 hours) + (1.5 times of the wage for the
return total_Weekly_Wage overtime period); by calling calcWeeklyWage() function TWICE

# Assume
hourlyWage = 55
# Input
totalHours = # Keep the statement you have created in task 1
# Process
# KEEP Your function call for TASK 1
# Call the function for TASK 2.
Type here to call the calcWithOverTimeWage( ___ , ___ ) function
total_Weekly_Wage =
# Output
print(f'Wages for {totalHours} hours at ${hourlyWage} per hour are
${total_Weekly_Wage}')
Task 2 – Check your output
Inputs Outputs
hourlyWage FIXED in TASKS 1 and totalHours total_Weekly_Wage
2 calcWithOverTimeWage()
(will change in TASKS 3 and 4)
55 39.5 $2172.5
55 50 $3025.0
55 45 $2612.5
➢ Completion Reminder:
➢ After completing task 2, save your work in “employeeWage.py”
➢ Make sure the “employeeWage.py” file contains complete
code for task 2 calcWithOverTimeWage( ) and
code for task 1 calcWeeklyWage( ).
Task 3
Task 3 Scenario
➢ Let us now assume now, that there are three pay scales in
CUHK supermarket.
➢ Their total number of working hours in a week and the scale
point list are given in the table.

CUHK Supermarket Scale Point List

Employee Total worked hours Pay Scale Pay Scale Hourly Wage

A 42 3 3 80

B 38.5 2 2 70

C 45 1 1 55

➢ AGAIN, if any employee works overtime, i.e., more than 40


hours/week, the hourly wages of those extra hour(s) shall
increase by 50% applying to ALL pay scales/ranks.
calcWeeklyWage()
Task 3 Scenario Already done:
Pass the
Call calcWeeklyWage()
parameters
from task1
➢ In task 3, the idea is to use the pay
scale to determine weekly wage of
an employee. calcWithOverTimeWage()

➢ The program asks the user to enter


Pass the
parameters Call calcWithOverTimeWage()
➢ hours worked (as in Tasks 1 and 2) from task2
➢ a pay scale level, i.e., 1, 2, 3, etc.
➢ With pay scale information, the scaleWage() Define the scaleWage()
- Map hourly wage with pay scale function
program checks and picks the - Call the In task 3
corresponding hourly wage for the calcWithOverTimeWage()

employee.
Pass the
➢ Finally, by using hourly wage parameters

information and hours worked,


calculate the weekly wage.
#code to call the function
➢ Reminder: take into account OT Complete the code to
- Request parameters
too! call the function
- Call the function
Print and check the result
Task 3 Scenario

➢ We continue to work on the same file “employeeWage.py”.

➢ Read the Comments/TODO for TASK 3 CAREFULLY.

➢ Complete the “scaleWage()” function and calculate the


weekly wages in a week for an employee working in the
CUHK supermarket.
Task 3 FOLLOWS Task 2
def calcWeeklyWage(totalHours, hourlyWage):
## Completed in Task 1 ##
def calcWithOverTimeWage(totalHours, hourlyWage):
## Completed in Task 2 ##
def scaleWage(totalHours, payScale):

if payScale equals 3:
hourlyWage shall be 80
elif payScale equals 2:
hourlyWage shall be 70 Use if-elif statements and set
: hourly wage for the
:
corresponding pay scale
else: information according to
hourlyWage shall be -1 table in slide 22
print("Invalid data")

total_Weekly_Wage = Call the calcWithOverTimeWage( _____ , ______ )


return total_Weekly_Wage, hourlyWage
# Input
Keep this line from TASK 1 for inputting totalHours
totalHours =
payScale =
# Use the int( ) and input( ) function with a message "Please enter the pay
scale of the employee (1, 2, etc.) : ", to request an input from the user
# Process
# Call the function for TASK 3.
total_Weekly_Wage, hourlyWage =
Call the scaleWage( _____ , _____ ) function

# Output
print(f'Wages for {totalHours} hours at ${hourlyWage} per hour are
Task 3 – Check your output
and Submit
Inputs Outputs

totalHours payScale total_Weekly_Wage


42 1 $2365.0
45 2 $3325.0
35 3 $2800.0

➢ Submission Details:
➢ After completing task 3, save your work and submit the completed
“employeeWage.py” file on Blackboard
➢ Please note: Make sure the completed “employeeWage.py” file has
complete codes of tasks 1, 2 and 3.
Bonus Task 4
Task 4 Scenario
➢ In this task we introduce 4 employee roles with various pay scale.
➢ You are required to map the employee role to the pay scale, thus hourly
wage.
➢ Pay scale and hourly wage information are shown in the table below:
CUHK Supermarket Scale Point List (Same as TASK 3)
Employee role Pay scale Pay Scale Hourly Wage
manager 3 3 80
supervisor 2 2 70
accountant 1 1 55
worker 1

➢ Observe that "accountant" and "worker" have the same pay scale.
That means same pay scale can be assigned to more than one
employee roles.
➢ OT and all previous wage calculations shall be taken into account.
Bonus Task 4 Scenario
➢ If you attempt Bonus Task 4, you shall surrender part of your work in Task 3!!
➢ Your program asks the user for these inputs:
➢ number of hours worked in a week, keep the same line as in Tasks 1, 2 and 3

➢ COMMENT to DISABLE payScale input statement line in Task 3

➢ employee role that is a text input (str) containing a mixed-case word


Loading…
Create a new function named roleWage( ) that takes two arguments,
totalHours and employeeRole.
➢ Argument employeeRole is expected to be a single word in lower-case:
manager or supervisor or accountant or worker

➢ This function determines a payScale from employeeRole using if-elif-else;


set payScale to -1 if employeeRole is not matched

➢ This function calls scaleWage() function to obtain and return end results

Ø During Input and Testing, we shall convert the user input to lower-case
Ø E.g., name = "PeTer".lower() name will be "peter"

Ø E.g., "MaNagEr" "manager", by using the .lower() str function


Bonus Task 4 FOLLOWS Task 3

Step 3 Step 2

Pass the parameters Pass the parameter


calcWithOverTimeWage() scaleWage() roleWage()
Return result Return result

Step 6 Step 7

Pass the
Step 5 Return Pass the parameters
Step 4
result parameters

Step 1

# Input and adjust Task 3

calcWeeklyWage() # Process
- Request parameters
Step 8 is a line already given at the end, for ALL TASKS - Call the function
Print and check the result
Task 4 FOLLOWS Task 3
➢ Based on your COMPLETED work in file “employeeWage.py” that
contains all the complete codes of Tasks 1, 2 and 3.
➢ Read the Comments/TODO in the file and work on the Bonus Task
4 in the SAME file “employeeWage.py"
➢ Construct the roleWage() function that accepts the employee role
to determine the scale point and total weekly wage.
➢ Then, calculate the weekly wage of an employee by calling some
existing function. The expected output is shown in table below:

Inputs Outputs
totalHours employeeRole total_Weekly_Wage
37 manager $2960.0
41 supervisor $2905.0
45 accountant $2612.5
45 worker $2612.5
Bonus Task 4 - Submission

➢ After completing the Bonus Task 4, upload and submit


a SINGLE FILE “employeeWage.py” on Blackboard
➢ Make sure the submitted file contains complete code
of Bonus Task 4, including also the complete code of
Tasks 1, 2 and 3.
➢ Important:
➢ Attempt of Bonus Task 4 shall supersede and include ALL
your work in Tasks 1, 2 and 3.
➢ If you submitted an incorrect file to Blackboard, you may
get 0 marks. Make sure your functions in Tasks 1, 2 and 3
are intact and NOT corrupted in your Task 4 attempt.

You might also like