python虽然循环迭代不起作用



我是Python编程中的新产品,并且无法在几个小时内解决以下问题。我有包含四行的示例文件。我希望在文件行中找到两个输入,但是我的循环似乎并没有在所有行上迭代。这是文件的内容,元素被一个空间分开:

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

这是我的代码:

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

我希望输入例如Google和Microsoft并获取以下输出:

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

作为两个列表。但是,我的代码只能找到第一行,而不是其他行。你能告诉我为什么它不会在其他线上迭代。如何解决这个问题?预先感谢!

这是没有将代码从您的尝试急剧上更改的东西...它使用lower()title()来允许各种用户输入的资本化:

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)

示例用法:

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!

这是您只能仅提示用户的"两个术语":

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...")

示例用法:

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!

尝试以下代码。该代码标识找到哪个术语,并询问未找到的术语。

您没有正确地进行循环。当您的循环第一次执行时,它具有Google,term1包含Google,term2包含Microsoft。因此,它打印了Google,然后使用 sys.exit()弯腰执行。因此,当然会打印第一行而不是其他行。

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

最新更新