在 if 语句下将两个 for 循环合并在一起

Merging two for loops together under an if statement

我正在尝试将这两个代码合并在一起,以便最后一块可以将文本文档 "Payments" 附加到列表中。对于 "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 搜索客户编号并在屏幕上显示客户的所有信息。

这是两个程序中的第一个。也就是最终代码的"backbone"。我们称之为 A:

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)

我想将 B 插入到 if 语句中:if decision == "A" or decision == "a":

我对 for 循环感到困惑,因为 A 和 B 中都有一个 for 循环,这两者对于最终代码都是至关重要的。我无法在不中断任何一个 for 循环的情况下将 B 放入 A。

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_number 存在于一行中,那么您追加到列表并使用循环遍历列表以打印您想要的内容。您的解决方案是打印整行客户详细信息并每隔一行打印一次。

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()

这是输出:

I want to be able to create the final code to prompt the user to enter a customer number, then search through myList for the customer number and display all the information for the customer on the screen.

我对您的代码进行了一些修改,并放入了符合 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 所在的格式进行修改。