Search code examples
pythonloopspython-3.xsplitstrip

Merging two for loops together under an if statement


I am trying to merge these two codes together so that the final piece can append the text document "Payments" into a list. For each line of "Payments", I want it within a list in myList, so it would look something like this:

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

I want to be able to create the final code to prompt the user to enter a customer number, then search through myList for the customer number and display all the information for the customer on the screen.

This is the first of the two programs. It is the "backbone" of the final code. Let's call this 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

This is the second piece of code. Let's call this B:

myFile = open("Payments.txt")
myList = []
for item in myFile:
    print(item.strip())
    myList.append(item.strip().split(','))
myFile.close()
print(myList)

I want to insert B into the if statement: if decision == "A" or decision == "a":.

I am confused about the for loops as there is a for loop in A and B, both which are vital for the final code. I am unable to place B into A without disrupting either one of the for loops.

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

It displays the line from which the customer number is from, but it doesn't print the list.

Update

I want to be able to print the individual data from each line separately:

Customer number: E1234
Date of payment: 12/09/14
Payment amount: £440
Paid amount: £0

Solution

  • In your comment you mentioned you need to append to a list so I have modified my script to include that but your current answer was separating your A and B part but you had to combine them.

    The logic is, if customer_number exists in a line, then you append to list and use a loop to iterate through the list to print what you want. Your solution was printing the entire line of the customers details and also printing every other line.

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

    Here is the output:

    enter image description here