CS 115 Lab 4 Solved

34.99 $

Description

5/5 - (2 votes)

Lab Guide 04

Lab Objectives: Files and Modules

[Q1A] Write a python module, Q1_module.py that contains the following functions:
1. displayMenu(): the method should display the menu shown in the sample run, and return the value input by the user. The method should keep inputting a choice until a valid value (a,b,c,d) is entered.

2. findEmailByDomain() : takes a domain and an input file name as parameters. The function reads the file, finds the email addresses with the given domain. For each found email address, writes the user names to an output file with the name domain.txt, where the domain is the one passed as a parameter.

3. displayUsersHavingNumeric(): takes a domain as a parameter, and reads the file containing users from the given domain, and displays all users in the file with a numeric character in their user name.

Note: if no file from the given domain exists, a file error will occur. To prevent this, you can use a
try/except statement to your method as follows:
def displayUsersHavingNumeric(…) try:
#open file
#read the file data and do something with it except:
print(‘No such file exists’)
The try block tries to open the file and should include all statements to be executed if the file can be opened. If the file cannot be opened (error occurs), the except block will be executed instead.

[Q1B]
4. Write a python script that uses the functions defined in the module to display the menu, input data and execute the menu functionality.

Sample Run:

Menu:
(a) Find email by domain
(b) Display numeric userNames by domain
(c) EXIT

Enter choice: a
Enter domain to search: cs
4 users exist in cs domain

Menu:
(a) Find email by domain
(b) Display numeric userNames by domain
(c) EXIT

Enter choice: b Enter domain to search: cs smith123 kara2kus

Menu:
(a) Find email by domain
(b) Display numeric userNames by domain
(c) EXIT

Enter choice: b
Enter domain to search: fen
No such file exists

Menu:
(a) Find email by domain
(b) Display numeric userNames by domain
(c) EXIT

Enter choice: c Goodbye!

  • Lab4-e7lfmg.zip