【发布时间】:2020-04-07 10:31:11
【问题描述】:
我有一个已成功读入列表的文件,我有一个循环读取该列表的每一行,以查找名为customerID 的变量,它只是一个由 4 个数字组成的字符串。
我试图让 if 语句打印找到 customer ID 的列表的索引,以及该行的内容(索引)。
def searchAccount(yourID):
global idLocation
global customerlist
with open("customers.txt", "r") as f:
customerlist = [line.strip() for line in f]
IDExists = False
for line in customerlist:
if yourID in line:
IDExists = True
break
else:
IDExists = False
if IDExists == True:
print(customerlist.index(yourID))
【问题讨论】:
标签: python list indexing element