Python while 循环迭代不起作用
Python while loop iteration does not work
我是 Python 编程的新手,几个小时都无法解决以下问题。我有包含四行的示例文件。我希望在文件行中找到用户输入时获得两个输入,但是我的循环似乎没有遍历所有 lines.This 是文件的内容,元素由 [=25 分隔=]:
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()
and 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!
这里是如何只提示用户只输入 "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...")
用法示例:
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
我是 Python 编程的新手,几个小时都无法解决以下问题。我有包含四行的示例文件。我希望在文件行中找到用户输入时获得两个输入,但是我的循环似乎没有遍历所有 lines.This 是文件的内容,元素由 [=25 分隔=]:
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()
and 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!
这里是如何只提示用户只输入 "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...")
用法示例:
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