Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to modify a key value from a user inputs request

I am making a basic app where I let the user add, delete and edit the stock they have. I am trying to make it so I can let the user either ‘add’ or ‘remove’ which they decide and type it and the input captures their response. I don’t to use a load of if user_input = ‘add’ do this and then elif user_input == ‘remove’ do that. What would be the best way to go around this.

Here is my code for the edit inventory part;

    def edit_inventory(self):
        item_name = input('Enter item name: ')

        try:
            user_input = input('Type \'ADD\' or \'REMOVE\' to change quantity: ').lower()
            if user_input not in ['add', 'remove']:
                raise ValueError
            quantity = int(input(f'Enter how many you wish to {user_input}: '))
            if user_input == 'add':
                for item_name in self.stock.keys():
                    self.stock[item_name] += quantity
            elif user_input == 'remove':
                for item_name in self.stock.keys():
                    self.stock[item_name] -= quantity

        except KeyError:
            print('Item not in stock. Check current stock for current inventory.')
        except ValueError:
            print('Invalid response.')

I want to work around the if and elif to make it more efficient.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One approach is to use a dispatch table, like this:

def add_item(self, item):
    # code to add an item

def remove_item(self, item):
    # code to remove an item

def edit_inventory(self):
    cmd_table = {
        'add': self.add_item,
        'remove': self.remove_item,
    }
    item_name = input('Enter item name: ')
    cmd = input("Type 'ADD' or 'REMOVE' to change quantity: ").lower()
    cmd_func = cmd_table[cmd]  # raises KeyError automatically if cmd is invalid
    # add whatever other stuff, e.g. getting quantity
    cmd_func(item)

This way if you have a large number of commands, you just need one entry in the dictionary per command, rather than a bunch of if/elif. You also get reasonable behavior around invalid entries for free; you can use [] and get a KeyError, or you can use .get() and supply a default function, or you can test if cmd in cmd_table.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading