2017-04-24 11 views
-3

一個循環我目前正在對這個代碼:控制在Python

import time 
import webbrowser 

while True: 
    user_inputq3 = raw_input("Which model of phone is it? ") 
    import csv 
    words=user_inputq3.split(" ") 
    reader=csv.reader(open("task3worksheet.csv")) 
    problemlist=[row for row in reader] 
    for x in range(0,len(words)): 
     for y in range(0,len(problemlist)-1,2): 
      if(words[x].lower() in problemlist[y]): 
       print(problemlist[y+1][0]) 
       break 
      elif user_inputq3 == "exit": 
       print("The programme will now shut down.") 
       time.sleep(5) 
       exit(0) 
       break 
      else: 
       print("This troubleshooting programme will only support iPhone 3-7.") 
       print("Make sure you type in iphone then the model of iPhone. (e.g iphone4)") 
       print("If you do not have these versions of the iPhone, please type in exit when prompted with the question again.") 
       break 

的「task3worksheet」是一個Excel工作表,這是充滿關鍵字和響應。因此,例如,當輸入「iPhone4」時,應該出現「下一個問題」。但是,當我輸入「iPhone4」時,它會提出下一個問題並再次回到問題。我該如何做到這一點,以便它不會再次回到問題中,我希望它能夠在輸入有效輸入時結束當前循環,因此我可以繼續使用其他代碼。

+0

請參見[如何提問](http://stackoverflow.com/help/how-to-ask),並幫助中心的關於創建[MCVE指導]。特別是,問題的標題應該是針對個人問題的,並且代碼應該是儘可能小的事情來展示問題或錯誤 - 刪除所有不相關的內容。 StackOverflow不是一個論壇,而是一個問答網站 - 我們的目標是創建一個有大家可以使用的答案的典型問題數據庫;一個對任何人都沒有幫助的問題,但是你沒有朝着這個目標努力。 –

+0

您可以添加一個變量,告訴您是否已經打印了任何回覆。這應該是最簡單的 – fulaphex

回答

0

我添加了一個變量,告訴我如果我已經回答了問題,如果我沒有打破外部循環。 改良版在評論一個建議:

import time 
import webbrowser 

answered = False 
while not answered: 
    user_inputq3 = raw_input("Which model of phone is it? ") 
    import csv 
    words=user_inputq3.split(" ") 
    reader=csv.reader(open("task3worksheet.csv")) 
    problemlist=[row for row in reader] 
    for x in range(0,len(words)): 
     if answered: 
      break 
     for y in range(0,len(problemlist)-1,2): 
      if(words[x].lower() in problemlist[y]): 
       print(problemlist[y+1][0]) 
       answered = True 
       break 
      elif user_inputq3 == "exit": 
       print("The programme will now shut down.") 
       time.sleep(5) 
       exit(0) 
       break 
      else: 
       print("This troubleshooting programme will only support iPhone 3-7.") 
       print("Make sure you type in iphone then the model of iPhone. (e.g iphone4)") 
       print("If you do not have these versions of the iPhone, please type in exit when prompted with the question again.") 
       break 
+0

這仍然回到「哪種型號的手機?」這個問題上,甚至在識別出正確的輸入和正確的解決方案之後。 –

+0

更進一步,將變量聲明爲更高的範圍並在while循環中檢查它 – fulaphex