简体   繁体   中英

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!

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!

Try the following code. This code identifies which term is found and asks the term which is not found.

You are not making your loop properly. When your loop executes first time it has Google and term1 contains Google and term2 contains Microsoft. So it prints Google and after that you are stooping the execution using sys.exit() . So of course it prints the first line not the others.

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

NeedToExecute = 0
term1 = False
term2 = False

list1 = None
list2 = None

while NeedToExecute != 2:

    if(term1 != True):
        term1 = input("Enter the first term:")

    if(term2 != True):
        term2 = input("Enter the second term:")

    for i in results:

        if term1 in i :
            NeedToExecute = NeedToExecute + 1
            term1 = True
            list1 = i
            print(i)

        if term2 in i :
            NeedToExecute = NeedToExecute + 1
            term2 = True
            list2 = i
            print(i)

print list1
print list2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM