Search code examples
pythonfor-loopwhile-loopuser-inputlis

Python while loop iteration does not work


I am a new one in Python programming and have not been able to solve the following problem for hours. I have sample file which contains four lines. I wish to get two inputs from user by the time it is found in the file line, however my loops do not seem to iterate over all the lines.This is the content of the file, elements are separated by a space:

Google 1 2  
Apple 13 17  
Microsoft 22 8  
Verizon 6 15

And this one is my code:

import sys
with open('manylines.txt','r') as myfile:
    results = [line.split() for line in myfile]

    for i in results:
        print(i)


        term1=input("Enter the first term:")
        while  term1 not in i :
            print("Term not found,please try again:")
            term1 = input("Enter the first term:")

        term2 = input("Enter the second term:")
        while term2 not in (i) :
            print("Term not found,please try again:")
            term2 = input("Enter the second term:")
        print(i)
        print(i)
        sys.exit()

I wish to input for example Google and Microsoft and get following output:

['Google','1','2']  
['Microsoft','22','8']

as two lists. However, my code only finds the first line and not the others. Could you tell me why it does not iterate over other lines, please. How to fix this? Thanks in advance!


Solution

  • Here is something that does not change the code from your attempt to drastically... It makes use of lower() and title() to allow for various capitalization's of user input:

    import sys
    
    with open('data.txt','r') as f:
        results = [line.split() for line in f]
    
    while True:
        found = False
        term = input("Enter a company to search or type exit: ")
        if term.lower() == "exit":
            break
        for record in results:
            if record[0] == term.title():
                print(record)
                found = True
                break
        if not found:
            print("Term not found, please try again...")
    
    print("Goodbye!")
    sys.exit(0)
    

    Example Usage:

    Enter a company to search or type exit: Google
    ['Google', '1', '2']
    Enter a company to search or type exit: microsoft
    ['Microsoft', '22', '8']
    Enter a company to search or type exit: Amazon
    Term not found, please try again...
    Enter a company to search or type exit: Exit
    Goodbye!
    

    Here is how you can only prompt the user for only "two terms":

    out1, out2 = [], []
    term_count = 1
    while term_count < 3:
        found = False
        term = input("Enter term %d or type exit: " % term_count)
        if term.lower() == "exit":
            break
        for record in results:
            if term_count == 1 and record[0] == term.title():
                print(record)
                out1 = list(record)
                found = True
                term_count += 1
                break
            if term_count == 2 and record[0] == term.title():
                print(record)
                out2 = list(record)
                found = True
                term_count += 1
                break
        if not found:
            print("Term not found, please try again...")
    

    Example Usage:

    Enter term 1 or type exit: Amazon
    Term not found, please try again...
    Enter term 1 or type exit: Google
    ['Google', '1', '2']
    Enter term 2 or type exit: Microsoft
    ['Microsoft', '22', '8']
    Goodbye!