CPSC223 Lab 1

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment

Description

5/5 - (1 vote)

Laboratory Objectives

1. Write a Python program using:
i. lists
ii. loops
iii. conditional statements
iv. input/output
v. modules
vi. functions

Program Instructions

1. Write a Python program that performs as a Employee Contact List which contains a list of
contacts that can be modified or deleted.

2. Create a contacts module to meet the following requirements:
i. Create a file named contacts.py.
ii. Add a comment at the top of the file which indicates your name, date and the purpose
of the file.

iii. Note: All contact lists within this module should assume the list is of the form: [[“first
name”,”last name”],[“first name”,”last name”],…]
iv. Define a function named print_list to meet the following requirements:

a. Take a contact list as a parameter.
b. Implement a docstring with a simple sentence that describes the function.
c. Print a header for the printout which indicates the list index number, the first
name, and the last name column headers.

d. Loop through the contact list and print each contact on a separate line
displaying: the list index number, the contact first name, and the contact last
name. Assuming i is the index value and contacts is the name of the list, the
following will format the
output: print(f'{str(i):8}{contacts[i][0]:22}{contacts[i][1]:22}’)

v. Define a function named add_contact to meet the following requirements:
a. Take a contact list as a parameter.
b. Implement a docstring with a simple sentence that describes the function.
c. Prompt the user for the first name (note: look up and use the input() function to
prompt and get input from the user).

d. Prompt the user for the last name.
e. Add the contact to the list.
f. Return the updated list.

vi. Define a function named modify_contact to meet the following requirements:
a. Take a contact list as a parameter.

b. Implement a docstring with a simple sentence that describes the function.
c. Prompt the user for the list index number to modify.
d. Prompt the user for the first name.
e. Prompt the user for the last name.

f. Modify the contact list at the index value.
g. Return the updated list.

vii. Define a function named delete_contact to meet the following requirements:
a. Take a contact list as a parameter.
b. Implement a docstring with a simple sentence that describes the function.
c. Prompt the user for the list index number to delete.

d. Delete the contact at the index value.
e. Return the updated list.

3. Create a main driver program to meet the following requirements:
i. Create a file named main.py.
ii. Add a comment at the top of the file which indicates your name, date and the purpose
of the file.

iii. Import the contacts module. The first line of code in the main.py file should be:
from contacts import *
iv. Define a variable to use for the contact list.
v. Implement a menu within a loop with following choices:

a. Print list
b. Add contact
c. Modify contact
d. Delete contact
e. Exit the program
vi. Prompt the user for the menu choice and call the appropriate contacts function or exit
the program.

4. Check all the values entered by the user. For example, if the index entered by the user during
the delete_contact() execution is not within the range of the contact list, print out “invalid index
number” and return the unedited list. Similarly, implement appropriate error-checks on all
user’s inputs

Typical input and output for the program:
*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 2
Enter first name: Richard
Enter last name: Stallman
*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 2
Enter first name: Bill
Enter last name: Gates

*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 2
Enter first name: Steve
Enter last name: Jobs

*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 1

================== CONTACT LIST ==================
Index First Name Last Name
====== ==================== ====================
0 Richard Stallman
1 Bill Gates
2 Steve Jobs
*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 3
Enter the index number: 2
Enter first name: Tim
Enter last name: Cook

*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 1

================== CONTACT LIST ==================
Index First Name Last Name
====== ==================== ====================
0 Richard Stallman
1 Bill Gates
2 Tim Cook
*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 3
Enter the index number: 5
Invalid index number.
*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 4
Enter the index number: 1

*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 1

================== CONTACT LIST ==================
Index First Name Last Name
====== ==================== ====================
0 Richard Stallman
1 Tim Cook
*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 4
Enter the index number: 5
Invalid index number.

*** EMPLOYEE CONTACT MAIN MENU
1. Print list
2. Add contact
3. Modify contact
4. Delete contact
5. Exit the program
Enter menu choice: 5

Submission
Test your program to make sure all the menu selections work as intended (print, add contact,
modify contact, delete contact, exit). Also make sure the user’s inputs are validated. When
completed, upload the two below files to Canvas:
– main.py
– contacts.py