Home > Software design >  Why is that find_customer_by_name function only search the first customer?
Why is that find_customer_by_name function only search the first customer?

Time:12-23

Example of customers that I want to find from my list:

{'Customers': [
    {"Customer's ID": '001', "Customer's Name": 'dor', "Customer's City": 'london', "Customer's age": '26'}, 
    {"Customer's ID": '002', "Customer's Name": 'John Cena', "Customer's City": 'New York', "Customer's age": '45'},
    {"Customer's ID": '003', "Customer's Name": 'Tony Stark', "Customer's City": 'Holywood', "Customer's age": '39'}
]}

My code from Customers module to handle the customers system:

    def find_customer_by_name(customer_name, customers_library):

    """
    A search function that search customer in library by his name
    :param customer_name: Customer's name'
    :param customers_library: a dict with all customers in the library
    """

    customers_temp_library = copy.deepcopy(customers_library)
    if customer_name in customers_temp_library["Customers"][0]["Customer's Name"]:
        return f"{customer_name} is in the customers library list"

The code in main:

    if identifier == '3':  # Choosing to find customer (by name)
       print("Enter customer's name you would like to find: ")
       customer_name = input()
       print(find_customer_by_name(customer_name, customers_library))

CodePudding user response:

Because you are not using the iteration process to check all records, the following modification uses the loop to check all records for provided customer name

def find_customer_by_name(customer_name, customers_library):

    """
    A search function that search customer in library by his name
    :param customer_name: Customer's name'
    :param customers_library: a dict with all customers in the library
    """

    customers_temp_library = copy.deepcopy(customers_library)
    for i in range(len(customers_temp_library["Customers"])):
        if customer_name in customers_temp_library["Customers"][i]["Customer's Name"]:
            return f"{customer_name} is in the customers library list"

Also a optimized form of the code can be following by removing copy as you are not modifying the data so its safe to only iterate the collection and check your required name

data = {'Customers': [{"Customer's ID": '001', "Customer's Name": 'dor', "Customer's City": 'london', "Customer's age": '26'}, {"Customer's ID": '002', "Customer's Name": 'John Cena', "Customer's City": 'New York', "Customer's age": '45'},{"Customer's ID": '003', "Customer's Name": 'Tony Stark', "Customer's City": 'Holywood', "Customer's age": '39'}]}

def find_customer_by_name(customer_name, customers_library):

    """
    A search function that search customer in library by his name
    :param customer_name: Customer's name'
    :param customers_library: a dict with all customers in the library
    """

    for customer in customers_library["Customers"]:
        if customer_name == customer["Customer's Name"]:
            return f"{customer_name} is in the customers library list"
    
    return f"{customer_name} is not in the list"

test = ['Tony Stark', 'Foo', 'Bar', 'John Cena']
for name in test:
    print(find_customer_by_name(name, data))

Output is following

enter image description here

  • Related