【问题标题】:Merging two for loops together under an if statement在 if 语句下将两个 for 循环合并在一起
【发布时间】:2016-04-27 20:21:15
【问题描述】:

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

myList = [['E1234','12/09/14','440','A','0']['E3431','10/01/12','320','N','120']['E0987','04/12/16','342','A','137']]

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

这是两个程序中的第一个。它是最终代码的“主干”。我们称它为 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)

我想在 if 语句中插入 B:if decision == "A" or 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 会做什么?只需打印 Payments.txt 的内容?
  • B 将 Payments.txt 的每一行添加到 myList 中的单个列表中。我希望 B 在 A 中的 if 语句中,因此它将 Payments 附加到 myList,在列表中找到 customer_number 并显示该特定行
  • 所以这样做的全部目的是显示包含 customer_number 的行?为什么还要打扰myList
  • 规则要求在此作业中使用此代码中的列表。抱歉,我之前没有解释过。
  • 好的,我修改了我的解决方案来解决这个问题并做你想做的事情:) 你在正确的轨道上,但你没有打印正确的值

标签: python loops python-3.x split strip


【解决方案1】:

在您的评论中,您提到您需要附加到一个列表中,所以我修改了我的脚本以包含它,但您当前的答案是将您的 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()

这是输出:

【讨论】:

  • 为什么要循环两次文件的内容?
  • 它没有用。它打印了每一行,然后显示将其附加到myList
【解决方案2】:

我希望能够创建最终代码来提示用户输入 客户编号,然后在 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 的格式进行修改。

【讨论】:

    猜你喜欢
    • 2018-03-27
    • 2021-09-18
    • 1970-01-01
    • 2012-11-03
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    相关资源
    最近更新 更多