May-13-2019, 03:42 PM
Hello, I recently created a makeshift 'My Contact' program that is completely text based. I'm wondering if it is possible to turn this code into Object Oriented Programming, and if yes would the code become much better in terms of organization?
def main(): #init variables users = [{"name": 'John Doe', "phone": '123-456-7890', "address": '1000 Constitution Ave'}] loop = True; while loop: #Main menu options print('Main Menu') print('1. Display Contact Names') print('2. Search For Contacts') print('3. Edit Contact') print('4. New Contact') print('5. Remove Contact') print('6. Exit') selection = input('Enter a # form the menu: ') #Display contact names if (selection == "1"): for index in range(len(users)): print("Name: " + users[index]["name"]) #Search for contacts if (selection == "2"): searchname = input('What is the name of your contact: ') for index in range(len(users)): if (users[index]["name"] == searchname): print("Name: " + users[index]["name"]) print("Address: " + users[index]["address"]) print("Phone: " + users[index]["phone"]) #Edit contacts if (selection == "3"): searchname = input('What is the name of the contact that you want to edit: ') for index in range(len(users)): if (users[index]["name"] == searchname): users.pop(index) name = input('What is your name: ') address = input('What is your address: ') phone = input('What is your phone number: ') users.append({"name": name, "phone": phone, "address": address}) #New contact if (selection == "4"): name = input('What is your name: ') address = input('What is your address: ') phone = input('What is your phone number: ') users.append({"name": name, "phone": phone, "address": address}) #Remove contact if (selection == "5"): searchname = input('What is the name of the contact that you want to delete: ') for index in reversed(range(len(users))): if (users[index]["name"] == searchname): users.pop(index) print(searchname, 'has been removed') #Exit the program if (selection == "6"): print('Thank you for using the contact book, have a nice day') print('Carson147 2019©, All Rights Reserved') loop = False; main()