Untitled

 avatar
unknown
python
4 years ago
1.6 kB
6
Indexable
class UserDictionary():
    def __init__(self):
        self.user_dict = dict()
        self.menuoptions()
    

    def menuoptions(self):
        options = {
            "1": self.createentry,
            "2": self.readentries,
            "3": self.updateentry,
            "4": self.deleteentry
        }

        choice = input(
            """Which operation would you like to perform\n
            1. Create Entry\n
            2. Read Entries\n
            3. Update Entry\n
            4. Delete Entry\n\n
            Choice: """)
        if choice not in options: self.menuoptions()
        options[choice]()


    def createentry(self):
        key = input("What key would you like to insert?: ")
        value = input("What value would you like to insert?: ")

        print(f"\n{key}:{value} pair has been added\n")
        self.user_dict[key] = value
        self.menuoptions()


    def updateentry(self):
        key = input("What key would you like to update?: ")
        value = input("What do you want the new value to be?: ")

        print(f"\n{key}'s value has been updated to {value}\n")
        self.user_dict[key] = value
        self.menuoptions()
    

    def readentries(self):
        print()
        for k,v in self.user_dict.items():
            print(f"{k}:{v}")
        print()
        self.menuoptions()
    

    def deleteentry(self):
        key = input("What key would you like to delete?: ")

        print(f"\n{key} has been deleted\n")
        del self.user_dict[key]

        self.menuoptions()


new_dict = UserDictionary()
Editor is loading...