Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在if语句下合并两个for循环_Python_Loops_Python 3.x_Split_Strip - Fatal编程技术网

Python 在if语句下合并两个for循环

Python 在if语句下合并两个for循环,python,loops,python-3.x,split,strip,Python,Loops,Python 3.x,Split,Strip,我正在尝试将这两个代码合并在一起,以便最后一个代码可以将文本文档“Payments”附加到列表中。对于“付款”的每一行,我希望它位于myList的列表中,因此它看起来像这样: myList = [['E1234','12/09/14','440','A','0']['E3431','10/01/12','320','N','120']['E0987','04/12/16','342','A','137']] 我希望能够创建最终代码,提示用户输入客户编号,然后通过myList搜索客户编号,并在屏

我正在尝试将这两个代码合并在一起,以便最后一个代码可以将文本文档“Payments”附加到列表中。对于“付款”的每一行,我希望它位于
myList
的列表中,因此它看起来像这样:

myList = [['E1234','12/09/14','440','A','0']['E3431','10/01/12','320','N','120']['E0987','04/12/16','342','A','137']]
我希望能够创建最终代码,提示用户输入客户编号,然后通过
myList
搜索客户编号,并在屏幕上显示客户的所有信息

这是两个计划中的第一个。它是最终代码的“主干”。让我们称之为:

print("Option A: Show a record\nOption Q: Quit")
decision = input("Enter A or Q: ")

if decision == "A" or decision == "a":
    myFile = open("Payments.txt")
    customer_number = input("Enter a customer number to view their details: ")
    record = myFile.readlines()
    for line in record:
        if customer_number in line:
            print(line)
    myFile.close()

elif decision == "Q" or "q":
    exit
这是第二段代码。我们称之为B:

myFile = open("Payments.txt")
myList = []
for item in myFile:
    print(item.strip())
    myList.append(item.strip().split(','))
myFile.close()
print(myList)
我想在if语句中插入B:
if decision==“A”或decision==“A”:

我对for循环感到困惑,因为在a和B中有一个for循环,这两个循环对最终代码都至关重要。我无法将B放入A而不中断其中一个for循环

print("Option A: Show a record\nOption Q: Quit")
decision = input("Enter A or Q: ")
myList = []

if decision == "A" or decision == "a":
    myFile = open("Payments.txt")
    customer_number = input("Enter a customer number to view their details: ")
    record = myFile.readlines()
    for line in record:
        for item in myFile:
            print(item.strip())
            myList.append(item.strip().split(','))
            print(myList)
        if customer_number in line:
            print(line)
    myFile.close()

elif decision == "Q" or "q":
    exit
它显示客户编号的来源行,但不打印列表

更新

我希望能够分别打印每行的单个数据:

Customer number: E1234
Date of payment: 12/09/14
Payment amount: £440
Paid amount: £0

在你的评论中,你提到你需要附加到一个列表中,所以我修改了我的脚本,将其包括在内,但你当前的答案是分开你的a和B部分,但你必须将它们合并

逻辑是,如果一行中存在customer_编号,则将其追加到列表中,并使用循环遍历列表以打印所需内容。您的解决方案是打印整行客户详细信息,并且每隔一行打印一次

print("Option A: Show a record\nOption Q: Quit")
decision = input("Enter A or Q: ").lower()
myList = []

if decision == "a":
    myFile = open("Payments.txt")
    customer_number = input("Enter a customer number to view their details: ")
    record = myFile.readlines()
    for line in record:
        if customer_number in line:
            myList.append(line.strip().replace("'","").split(','))
            for info in myList:
                print("Customer number: ", info[0])
                print("Date of payment: ", info[1])
                print("Payment Amount: ", info[2])
                print("Paid Amount: ", info[4])
    myFile.close()

elif decision == "q":
    exit()
以下是输出:

我希望能够创建提示用户输入的最终代码 客户编号,然后在myList中搜索客户编号 并在屏幕上为客户显示所有信息

我对您的代码做了一些修改,并加入了符合PEP-8标准的样板文件

def customer_payment(customer_number):
    """Edited Code. Purpose is to accept User Input for a customer number
    Search for Customer Number, and if Found, Display Information about Customer.
    """
    myList = []
    with open("Payments.txt") as myFile:  
        for line in myFile:
            if customer_number in line:
                print(line)
            for item in line:
                print(item.strip())
                myList.append(line.strip().split(','))


def main():
    decision = input("Option A: Show a record\nOption Q: Quit\nEnter A or Q: ")
    if decision.lower() == "a":
        customer_number = input("Enter a customer number to view their details: ")
        customer_payment(customer_number)

if __name__=="__main__":
    main()

这可能需要根据Payments.txt的格式进行修改。

为什么要在文件内容上循环两次?它不起作用。它打印了每一行,然后显示它将其附加到
myList
。如果我理解正确,A只打印出特定客户的详细信息,但B做什么?只需打印Payments.txt的内容?B将Payments.txt的每一行添加到myList中的单个列表中。我想在A中的if语句中包含B,这样它会将付款附加到myList中,在列表中找到customer_编号,并显示该特定行。这样做的全部目的是显示包含customer_编号的行?为什么还要费心于
myList
?规则要求在此代码中使用列表来完成此工作。很抱歉,我之前没有解释。好的,我修改了我的解决方案以修复此问题,并执行您想要的操作:)您的操作正确,但打印的值不正确