Home > Blockchain >  How to print out all rows that have a column equal to a certain value specified by the user?
How to print out all rows that have a column equal to a certain value specified by the user?

Time:11-18

I am trying to create some code where a user will type in a category value and if a specific column in a row is == to that category then it will display the row in the format below. For some reason, my code only finds the first row in my array that is equal to the column but skips over the rest of the rows even if the column is equal to the users input.

For example, if someone types in Development as the category.. It should all rows with Development set in column 7(ie. rows 1 and 5) Any help to figure out the issue is much appreciated

contact_list = [[1,'Athena','Brizell','Vitz','9 Northview Hill',5888510700,344-241-3276,'Development','4/27/2021','10/29/2021','Athena Brizell'],
[2,'Isadore','Ander','Wordtune','72660 Orin Road',4848283832,877-155-7495,'Office_fitting','4/25/2021','9/26/2021','Isadore Ander'],
[3,'Hannis','Matches','Realpoint','847 Schurz Park',9052187780,368-121-1215,'Office_fitting','1/8/2021','9/4/2021','Hannis Matches'],
[4,'Cindee','Breadon','Gigashots','3 Bultman Way',4543988898,155-423-5293,'Support','4/25/2021','6/29/2021','Cindee Breadon'],
[5,'Baron','Arnao','Thoughtstorm','856 Blackbird Road',9831465576,207-486-1010,'Development','4/30/2021','10/28/2021','Baron Arnao'],
[6,'Laureen','Kearney','Dablist','70525 Texas Terrace',6091413184,602-843-9500,'Office_fitting','3/3/2021','9/3/2021','Laureen Kearney']]

category = input('Please enter the category you wish to display contacts for (Development/Office_fitting/Support: ')

for row in contact_list:
    if row[7] == category:
        firstname = f'Firstname: {row[1]}\n'
        lastname = f'Lastname: {row[2]}\n'
        company = f'Company: {row[3]}\n'
        address = f'Address: {row[4]}\n'
        landline = f'Phone: {row[5]}\n'
        mobile = f'Mobile: {row[6]}\n'
        category = f'Category: {row[7]}\n'
        date_created = f'Date Created: {row[8]}\n'
        date_modified = f'Date Updated: {row[9]}\n'
        print(firstname, lastname, company, address, landline, mobile, category, date_created, date_modified)

CodePudding user response:

You changed your category variable value that you defined earlier from user input inside your for loop. To fix it, rename the category variable to something else eg category_row inside the for loop.

contact_list = [[1,'Athena','Brizell','Vitz','9 Northview Hill',5888510700,344-241-3276,'Development','4/27/2021','10/29/2021','Athena Brizell'],
[2,'Isadore','Ander','Wordtune','72660 Orin Road',4848283832,877-155-7495,'Office_fitting','4/25/2021','9/26/2021','Isadore Ander'],
[3,'Hannis','Matches','Realpoint','847 Schurz Park',9052187780,368-121-1215,'Office_fitting','1/8/2021','9/4/2021','Hannis Matches'],
[4,'Cindee','Breadon','Gigashots','3 Bultman Way',4543988898,155-423-5293,'Support','4/25/2021','6/29/2021','Cindee Breadon'],
[5,'Baron','Arnao','Thoughtstorm','856 Blackbird Road',9831465576,207-486-1010,'Development','4/30/2021','10/28/2021','Baron Arnao'],
[6,'Laureen','Kearney','Dablist','70525 Texas Terrace',6091413184,602-843-9500,'Office_fitting','3/3/2021','9/3/2021','Laureen Kearney']]

category = input('Please enter the category you wish to display contacts for (Development/Office_fitting/Support: ')

for row in contact_list:
    if row[7] == category:
        firstname = f'Firstname: {row[1]}\n'
        lastname = f'Lastname: {row[2]}\n'
        company = f'Company: {row[3]}\n'
        address = f'Address: {row[4]}\n'
        landline = f'Phone: {row[5]}\n'
        mobile = f'Mobile: {row[6]}\n'
        category_row = f'Category: {row[7]}\n'
        date_created = f'Date Created: {row[8]}\n'
        date_modified = f'Date Updated: {row[9]}\n'
        print(firstname, lastname, company, address, landline, mobile, category_row, date_created, date_modified)

CodePudding user response:

That's because you modified category here:

category = f'Category: {row[7]}\n'

to include a new line \n now & another string too, so 'Development' != 'Category: Development\n'

It's better to format the strings in the print itself rather in their binding:

for row in contact_list:
    if row[7] == category:
        firstname = row[1]
        lastname = row[2]
        company = row[3]
        address = row[4]
        landline = row[5]
        mobile = row[6]
        category = row[7]
        date_created = row[8]
        date_modified = row[9]
        print(
            f'Firstname: {firstname}\nLastname: {lastname}\nCompany: {company}\nAddress: {address}\nPhone: /{landline}\nMobile: {mobile}\nCategory: {category}\nDate Created: {date_created}\nDate Updated: {date_modified}')
  • Related