我试图将这两个代码合并在一起,以便最后一部分可以将文本文档“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
如果我理解正确公正打印出客户的具体细节,但什么b执行?只需打印Payments.txt的内容? – Bijan
B将每个Payments.txt行添加到myList中的单个列表中。我希望B在A中的if语句内,因此它会将Payments附加到myList,在列表中找到customer_number并显示该特定行 – User0123456789
因此,其目的是显示包含customer_number的行吗?为什么还要用'myList'? – Bijan