Question: Creating a Contact List using 2 Classes in Python. Please help, I've been stuck for days. The intent of this program is to manage a
Creating a Contact List using 2 Classes in Python.
Please help, I've been stuck for days.
The intent of this program is to manage a set of contacts. Each contact will have data associated with it:
- Id number/integer
- First Name string
- Last Name string
- Age number/integer
- Phone Number string
- Email string
- gender
- height
- weight
You must allow the customer to do the following actions on the contact list:
- List all contacts
- Add contact
- Delete contact
- Edit contact
- Exit program
This is what I have so far, However, I seem to be stuck on the MODIFY CONTACT MENU. I only have 2 options right now but whenever I try either of those options it just redisplays the MODIFY CONTACT MENU. It's like it's stuck in a loop and I'm not sure how to fix it.
`class Contact:
def __init__(self, p_id, first_name, last_name, contact_age, phone, email, gender, height, weight):
self.ID = p_id
self.first_name = first_name
self.last_name = last_name
self.contact_age = contact_age
self.phone = phone
self.email = email
self.gender = gender
self.height = height
self.weight = weight
class ContactList:
def __init__(self, contact_list_name = 'none', date_created = 'Febuary 17, 2021', contact_list = []):
self.contact_list_name = contact_list_name
self.date_created = date_created
self.contact_list = contact_list
def add_contact(self):
temp_list = []
print("Let's add a new person ")
temp_list.append(input("What is their first name?: "))
temp_list.append(input("What is their last name?: "))
temp_list.append(input("What is their age?: "))
temp_list.append(input("What is their phone number?: "))
temp_list.append(input("What is their email?: "))
contactlist.append(temp_list)
def change_name(self):
print(' CHANGE NAME')
name = input('Enter the name you would like to change: ')
new_name = input('Enter the new name: ')
item_removed = False
for first in self.first_name:
if(first.first_name == name):
first.first_name = new_name
item_removed = True
if not item_removed:
print('Name not found in contact list, nothing changed.')
def modify_contact(self):
contact_list = newList
modify_menu = (" MODIFY CONTACT MENU "
"1: Change their first name "
"2: Change their last name "
"3: Change their age "
"4: Change their phone number "
"5: Change their email "
"6: Change their gender "
"7: Change their height "
"8: Change their weight "
"0: exit, back to main menu ")
command = ''
while command != 0:
string = ''
print(modify_menu)
command = input('Choose an option: ')
while(command != '1' and command != '2' and command != '3' and command != '4' and command != '5' and command != '6' and command != '7' and command != '8'):
command = input('Choose an option: ')
if command == 1:
contact_list.change_name()
elif command == 2:
print(' CHANGE LAST NAME')
name = input('Enter the name you would like to change: ')
new_name = input('Enter the new name: ')
item_removed = False
for last in self.last_name:
if(last.last_name == name):
last.last_name = new_name
item_removed = True
if not item_removed:
print('Name not found in contact list, nothing changed.')
elif command == 3:
pass
elif command == 4:
pass
elif command == 5:
pass
elif command == 6:
pass
elif command == 7:
pass
elif command == 8:
pass
else:
print_menu(newList)
def print_menu(ContactList):
contact_list = newList
menu = (' MAIN MENU '
'a - Add person to contact list '
'r - Remove person from contact list '
'c - Change contact info '
'd - Display contact list '
'q - Quit ')
command = ''
while(command != 'q'):
string=''
print(menu)
command = input('Choose an option: ')
while(command != 'a' and command != 'r' and command != 'c' and command != 'd' and command != 'q'):
command = input('Choose an option: ')
if command == 'a':
contact_list.add_contact()
if command == 'r':
contact_list.remove_person()
if command == 'c':
contact_list.modify_contact()
if command == 'd':
contact_list.display_contacts()
if __name__ == "__main__":
contactlist = []
contact_list_name = str(input('What would you like your contact list to be called? '))
date_created = str(input('Enter the date it was created: '))
print()
print('Contact List Name:', contact_list_name)
print('Date created:', date_created)
newList = ContactList(contact_list_name, date_created)
print_menu(newList)`
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
