Python 使用for循环搜索列表元素时出现问题

Python 使用for循环搜索列表元素时出现问题,python,arrays,list,file,Python,Arrays,List,File,我试图搜索列表中的每个元素,以查看行中是否存在变量customerID。我有一条语句,它将文件customers.txt的每一行读取到一个列表中。我使用的方法是使用for循环查看列表的每一行(元素),以查看其中是否存在4号字符串。无论出于何种原因,程序似乎只是读取第一行并继续。这个问题附带的代码显然是无效的,因为在这一点上,我正在努力使它正常工作 我将customer ID设置为等于1112,因为它位于第一行之后的一行中(因此,如果它读取每一行,它应该会看到它确实存在) 这是一个函数的一部分,也

我试图搜索列表中的每个元素,以查看行中是否存在变量
customerID
。我有一条语句,它将文件
customers.txt
的每一行读取到一个列表中。我使用的方法是使用for循环查看列表的每一行(元素),以查看其中是否存在4号字符串。无论出于何种原因,程序似乎只是读取第一行并继续。这个问题附带的代码显然是无效的,因为在这一点上,我正在努力使它正常工作

我将customer ID设置为等于1112,因为它位于第一行之后的一行中(因此,如果它读取每一行,它应该会看到它确实存在)

这是一个函数的一部分,也是一个更大问题的一部分

代码:

def openAccount():
    accountType = str(input("What type of account do you wish to open, checking or savings? "))

#make the user's input lowercase to make checking it simpler
    accountType = accountType.lower() 

    while accountType not in ["checking", "savings"]:
        accountType = str(input("Please enter checking or savings, for a checking or savings account: "))
        accountType = accountType.lower()

    global customerID
    customerID = random.randint(1000, 9999) 
    customerID = 1112
    customerID = str(customerID)

    global customerlist
    with open("customers.txt", "r") as f:
        customerlist = [line.strip() for line in f]

#    data = [line.strip() for line in open("customers.txt", 'r')]
#    for line in data: 
#        while customerID in line: 
#            IDExists = True
#            if IDExists == True:
#                customerID = random.randint(1000, 9999)
#                customerID = str(customerID)
#                print("Your Customer ID is", customerID)
#        else: 
#            IDExists = False
#            print("Your Customer ID is", customerID)
#            break


    IDExists = False
    for line in customerlist: 
        if customerID in line: 
           IDExists = True 

        else: 
            IDExists = False 

    if IDExists == True: 
        customerID = random.randint(1000, 9999) 

    else: 
        print("Your customer ID is ", customerID)

customers.txt:

1111 John Doe 11 MyStreet MyCity 12345 NY 23000 200
1112 Kevin Moe 23 HisStreet HerCity 54321 MA 15 3500
1113 Mary Joe 78 SomeStreet NewTown 32451 PA 6500 12000



找到客户ID后,应使用
break
停止循环

for line in customerlist:
    if customerID in line:
        IDExists = True
        break # Stop the loop as soon as we find one, since we initialize IDExists to False, it should remain False if there's nothing found

或者更好的方法是,使用
any(customerID在customerlist中一行接一行)

每当您测试成员资格时,
集合通常会很好地完成。看起来您的ID是第一列,所以将它们收集到
集合中(不要忘记转换为
int
):

def创建客户集(文件):
打开(文件)作为fh:
客户=集合()
#迭代你的文件
对于fh中的线路:
#在空格上拆分,抓住第一个条目
#并转换为int
cus_id=int(line.split()[0])
customers.add(客户id)
回头客
customer\u id=create\u customer\u set('customers.txt')
然后,检查该集合中是否有内容是一个固定时间操作(不取决于集合的大小),而且速度很快。您可以使用
generate\u customer
方法为您生成新id:

def生成客户(客户ID):
#您的帐户类型代码可以转到这里
尽管如此:
客户id=random.randint(10009999)
#在这里进行成员资格测试,如果需要继续循环
#现场
如果客户id中的客户id:
持续
#否则,打破循环,您拥有有效的客户id
其他:
返回客户id

然后,您可以对返回的
客户\u id

执行其他操作。您能将其格式设置得更正确一点吗?包括函数定义吗?另外,您的customers.txt的一个小样本也会很有帮助well@C.Nivs更新你能分享你的整个计划吗?可能需要进行一些重构。对于这样一个简单的程序,使用全局变量和缺乏回报让我担忧。您是否愿意使用诸如csv或Pandas之类的库?
customers.txt
文件的格式可能有问题,您从哪里获取数据?此外,变量和函数名称应遵循带有下划线的
小写形式。