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

the find customer by name function doesn't work as well, it only searching the first customer that i'm addig, why is that?

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:

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

    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))

>Solution :

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

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