Reputation: 758
I am attempting to print a list within a list. I know, I have posted a similar question before - Trouble printing a list within a list, but this is a different issue within the same topic, so please don't flag this as duplicate. I have merged two codes together and this is the final code. In the if statement, I am attempting to print the details of a certain customer of which the user has entered as customer_number
. The code searches through myList
, which Payments.txt
has been appended to. The code can do all of this, but the problem is that it prints this out multiple times; 6 times to be precise:
[['E1234', '12/09/14', '440', 'A', '0']]
Customer number: E1234
Date of payment: 12/09/14
Payment Amount: 440
Paid Amount: 0
This is my code:
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 scores: ")
record = myFile.readlines()
for line in record:
myList.append(line.strip().split(','))
details = [x for x in myList if x[0] == customer_number]
if details:
print(details)
print("Customer number: ", details[0][0])
print("Date of payment: ", details[0][1])
print("Payment Amount: ", details[0][2])
print("Paid Amount: ", details[0][4])
myFile.close()
print(myList)
elif decision == "Q" or "q":
exit
Upvotes: 2
Views: 270
Reputation: 2510
You need to exit the loop when you find and print an answer.
if details:
print(details)
print("Customer number: ", details[0][0])
print("Date of payment: ", details[0][1])
print("Payment Amount: ", details[0][2])
print("Paid Amount: ", details[0][4])
break
Upvotes: 3