我正在尝试创建一些代码,用户将在其中键入类别值,如果行中的特定列是该类别的==,则它将以下面的格式显示该行。由于某些原因,我的代码只查找数组中与该列相等的第一行,而跳过其余行,即使该列与用户输入相等。
例如,如果有人键入“开发”作为类别..它应该在第7列中设置为Development的所有行(即第1行和第5行)非常感谢对解决此问题的任何帮助
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)发布于 2021-11-17 14:47:36
您从for循环中的用户输入更改了前面定义的category变量值。要解决这个问题,可以在for循环中将category变量重命名为其他名称,例如category_row。
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)发布于 2021-11-17 14:53:13
这是因为你在这里修改了category:
category = f'Category: {row[7]}\n'添加一个新的行\n now和另一个string,所以'Development' != 'Category: Development\n'
最好在print本身中格式化字符串,而不是在它们的绑定中格式化:
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}')https://stackoverflow.com/questions/70006030
复制相似问题