2017-02-16 172 views
1

我是Python編程中的一名新成員,幾個小時後一直無法解決以下問題。我有包含四行的示例文件。我希望在文件行中找到來自用戶的兩個輸入,但是我的循環似乎無法遍歷所有行。這是文件的內容,元素由空格分隔:Python while循環迭代不起作用

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','1','2'] 
['Microsoft','22','8'] 

兩個列表。但是,我的代碼只找到第一行,而不是其他的。你能告訴我爲什麼它不會迭代其他線路,請。如何解決這個問題?提前致謝!

+0

您對第一學期和第二學期的價值是什麼? – lmiguelvargasf

+2

你在for循環中調用了'sys.exit',所以程序總是會在第一次迭代時終止。 –

+0

值是例如谷歌和微軟@lmiguelvargasf –

回答

0

下面是一些不將代碼從你的企圖徹底改變......它利用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! 
+0

非常感謝你@ shash678。還有一件事我想問。我希望只提示兩個輸入,如我的代碼所示。你能告訴我該怎麼做嗎?我的意思是,有一次它檢查兩個輸入,當它得到這兩個找到的項目時,它會按照你所顯示的和程序出口來打印這兩個適當的列表。你能說明一下嗎? –

+0

@python_beginner查看我的更新回答 – shash678

+0

謝謝@ shash678。我想問的最後一件事。這大概是我所尋找的,但是有可能我爲它們分配了兩個變量。我的意思是,如果Google發現,將輸出['Google','1','2']分配給out1並將微軟輸出['Microsoft','22','8']分配給out2。我的意思是我希望將它們作爲兩個列表,然後在程序的其他部分使用這些列表作進一步的用途。它怎麼能做到?我非常感謝你的幫助! –

0
嘗試

下面的代碼。此代碼標識找到哪個術語,並詢問找不到的術語。

您沒有正確製作循環。當您的循環首次執行時,它具有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 
+0

感謝@Ujjaval Moradiya的回答。但是你能告訴我如何爲這兩個列表分配兩個變量。讓我們說第一個變成list1,第二個變成list2。我希望將這些列表用於我的程序中的進一步目的。非常感謝你的幫助! –

+0

是的,你可以做到這一點。你爲什麼不這樣想。這並不困難。檢查我的更新代碼 –

+0

是的,我瞭解它,定義列表,然後在那裏添加結果。非常感謝你的幫助! –