Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Intro to Computer Programming - Python Programming Question: Address book. Write

ID: 3709676 • Letter: I

Question

Intro to Computer Programming - Python Programming Question:

Address book. Write a program to act as someone's address book. For each contact, it should be able to store (in a file) first & last names, phone number, e-mail address, and home address. The program should provide the following operations:

Add contact (enter information for a new person — first & last name are required, but any of the remaining fields could be blank)

Display contacts (show information for all contacts, ideally sorted by last name)

Search contacts (show all contacts whose first or last name contains some search text)

E-mail list (print a comma-separated list of all e-mail addresses, appropriate for copying & pasting into addresses of an e-mail)

Explanation / Answer

Solution:

code:

Note: Please mind the indentation.

import os.path

def main():

# filename for storing addresses file

filename = 'address.dat'

# displaying a menu of options until user chooses quit

while True:

print('1. Add Contact')

print('2. Display Contacts')

print('3. Search Contacts')

print('4. E-mail list')

print('5. Quit')

# taking user choice

print('Enter your choice(1/2/3/4/5) ? ')

choice = int(input().strip())

# on choice 5 the loop will break

if choice == 5:

break

if choice == 1:

while True:

# taking firstname, lastname, phone, address and email with first name and last name as mandatory

(fname, lname, pno, addr, email) = ('', '', '', '', '')

print('Enter first name: ')

fname = input()

print('Enter last name: ')

lname = input()

print('Enter phone number: ')

pno = input()

print('Enter address: ')

addr = input()

print('Enter email: ')

email = input()

# mandating first and last name

if fname and lname:

# checking if file exists, if not we create and write the read contents

if os.path.exists(filename):

fp = open(filename, "a")

fp.write(fname + ' ' + lname + ' ' + pno + ' ' + addr + ' ' + email + ' ')

fp.close()

else:

fp = open(filename, "w")

fp.write(fname + ' ' + lname + ' ' + pno + ' ' + addr + ' ' + email + ' ')

fp.close()

break

else:

print('firstname and lastname could not be empty, others are optional')

print('please re-enter')

elif choice == 2:

# before displaying file contents checking if file exists

if os.path.exists(filename):

fp = open(filename, "r")

for each_line in fp:

print(each_line)

fp.close()

else:

print(filename, 'does not exist, please add contacts then retry')

elif choice == 3:

# checking if file exists then taking search text as input and searching the file

if os.path.exists(filename):

fp = open(filename, "r")

print('Enter search text? ')

srch_txt = input().strip()

is_found = 0

# searching in file

for each_line in fp:

line_arr = each_line.strip().split(' ')

fname, lname = line_arr[0], line_arr[1]

if srch_txt in fname or srch_txt in lname:

is_found = 1

print(each_line)

if not is_found:

print('Given search text not found in first name or last name')

fp.close()

else:

print(filename, 'does not exist, please add contacts then retry')

elif choice == 4:

# checking if file exists

if os.path.exists(filename):

fp = open(filename, "r")

email_arr = []

# grabbing email list from the file

for each_line in fp:

line_arr = each_line.strip().split(' ')

if '@' in line_arr[-1]:

email_arr.append(line_arr[-1])

if email_arr:

print(','.join(email_arr))

else:

print('No emailIds exists in', filename)

fp.close()

else:

print(filename, 'does not exist, please add contacts then retry')

if __name__=='__main__':

main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote