2016-04-27 283 views
2

我试图将这两个代码合并在一起,以便最后一部分可以将文本文档“Payments”附加到列表中。对于“付款”的每一行,我希望它在myList名单内,所以它看起来是这样的:在if语句下将两个for循环合并在一起

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 
+0

如果我理解正确公正打印出客户的具体细节,但什么b执行?只需打印Payments.txt的内容? – Bijan

+0

B将每个Payments.txt行添加到myList中的单个列表中。我希望B在A中的if语句内,因此它会将Payments附加到myList,在列表中找到customer_number并显示该特定行 – User0123456789

+0

因此,其目的是显示包含customer_number的行吗?为什么还要用'myList'? – Bijan

回答

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

这里是输出:

enter image description here

+0

为什么你循环两次文件的内容? –

+0

它没有工作。它打印每一行,然后显示它将它附加到'myList'。 – User0123456789

+0

谢谢,这工作! – User0123456789

0

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

我对代码进行了一些修改,并将其放入符合PEP-8的Boilerplate中。

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是格式进行修改。

+0

它在自己的行上打印Payments.txt的每个字符... – User0123456789