我正在尝试将这两个代码合并在一起,以便最终的部分可以将文本文档“付款”附加到列表中。对于每行“付款”,我希望它在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
答案 0 :(得分:1)
在你的评论中,你提到你需要附加到列表中,所以我修改了我的脚本以包含它,但你当前的答案是分开你的A和B部分,但你必须将它们组合起来。< / p>
逻辑是,如果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()
这是输出:
答案 1 :(得分: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所在的格式进行修改。