Recap of User Defined Functions¶
As we have gone through all the key concepts related to User Defined Functions, let us recap them.
Defining the function with parameters
Relevance of Doc Strings
Returning one or more values
Defining default values to the parameters
Passing argument by position
Keyword arguments or passing argument by name
Different types of special arguments and how they are passed.
Varying arguments are passed as tuple
Varying keyword arguments are passed as dict
Come back to this once you are done with collections where processing tuples, lists, dicts etc are extensively covered. When you are comfortable, create a function called as add_employee which will put all the concepts related to User Defined Functions in action.
Function should take employee_id, employee_name, salary, phone_numbers (variable number), degrees (variable keyword arguments) as parameters.
We should be able to pass multiple phone numbers as argument for phone_numbers
Degrees should be with specialization. There can be one or more degrees with specializations with keys bachelors, masters, executive, doctorate.
Make sure salary is defaulted to 3000. If salary is passed and if it is less than 3000 print a message Invalid salary: {salary}, salary should be at least 3000
To get invalid phone count, create a function get_invalid_phone_count which takes employee_id and varrying phone numbers as argument. The function should return employee_id and invalid_phone_count.
A phone number which have 10 digits or characters is valid otherwise it is invalid.
Call get_invalid_phone_count and check if invalid_phone_count is greater than 0. If invalid phone count is greater than 0, print a message {l_invalid_count} phone numbers out of {len(phone_numbers)} are not valid
Get count of degrees by processing variable keyword argument. If there are any invalid degrees print One or more degrees are not valid
If all the values passed as arguments are valid print Employee {employee_id} with {number} of degrees is successfully added and his salary is {}
def get_invalid_phone_count(employee_id, *phone_numbers):
invalid_count = 0
for phone_number in phone_numbers:
if len(phone_number) != 10:
invalid_count += 1
return employee_id, invalid_count
def add_employee(employee_id, employee_name, *phone_numbers, salary=3000, **degrees):
degree_types = ('bachelors', 'masters', 'executive', 'doctorate')
invalid_degree_flag = False
invalid_salary_flag = False
l_employee_id, l_invalid_count = get_invalid_phone_count(employee_id, *phone_numbers)
if l_invalid_count != 0:
print(f'{l_invalid_count} phone numbers out of {len(phone_numbers)} are not valid')
if salary < 3000:
invalid_salary_flag = True
print(f'Invalid salary: {salary}, salary should be at least 3000')
for degree_key in degrees:
if degree_key not in degree_types:
invalid_degree_flag = True
if l_invalid_count != 0 or invalid_degree_flag or invalid_salary_flag:
if invalid_degree_flag: print('One or more degrees are not valid')
return
print('Employee {} with {} degrees is successfully added and his salary is {}'.format(employee_id, len(degrees), salary))
return
add_employee(1, 'IT', '1234567890', '1234567890', salary=5000, b='B. Sc', m='M. C. A')
One or more degrees are not valid
add_employee(1, 'IT', '12345678', '1234567890', salary=5000, b='B. Sc', masters='M. C. A')
1 phone numbers out of 2 are not valid
One or more degrees are not valid
add_employee(1, 'IT', '12345678', '1234567890', salary=2000, bachelors='B. Sc', masters='M. C. A')
1 phone numbers out of 2 are not valid
Invalid salary: 2000, salary should be at least 3000
add_employee(1, 'IT', '1234567890', '1234567890', salary=5000, bachelors='B. Sc', masters='M. C. A')
Employee 1 with 2 degrees is successfully added and his salary is 5000
def add_employee(employee_id, employee_name, *phone_numbers, salary=3000, **degrees):
"""Example using pre defined exception ValueError"""
degree_types = ('bachelors', 'masters', 'executive', 'doctorate')
try:
l_employee_id, l_invalid_count = get_invalid_phone_count(employee_id, *phone_numbers)
if l_invalid_count != 0 or salary < 3000:
raise ValueError
for degree_key in degrees:
if degree_key not in degree_types:
raise ValueError
print('Employee {} with {} degrees is successfully added and his salary is {}'.format(employee_id, len(degrees), salary))
except ValueError as ve:
print('Either one or more phone numbers are not valid or invalid salary, salary should be at least 3000 or one or more degrees are not correct')
add_employee(1, 'IT', '1234567890', '1234567890', salary=5000, b='B. Sc', masters='M. C. A')
Either one or more phone numbers are not valid or invalid salary, salary should be at least 3000 or one or more degrees are not correct
add_employee(1, 'IT', '1234567890', '1234567890', salary=5000, bachelors='B. Sc', masters='M. C. A')
Employee 1 with 2 degrees is successfully added and his salary is 5000