2016-09-14 47 views
0

代碼:Python的錯誤爲GCSE實踐

from array import * 
from math import * 
from string import * 
from random import * 
from time import * 

arrayQueryCategories = [["cracked","screen","display","smashed","broken","dropped"],#BrokenScreen 
         ["wet","damp","moist","water","soaked","liquid"],#WetPhone 
         ["charging","power","battery","plug","won't"],#NotCharging 
         ["turn","on","off","activate","black"],#WontTurnOn 
         ["buttons","stuck","press","home","lock","volume","mute"],#ButtonsBroken 
         ["locked","out","time","wait","code","password","stuck","unlock","passcode"]]#LockedOut 

def defSearchFor(): 
    global Result 
    Result = [] 
    Query = input("Search Term: ") 
    Query = Query.lower() 
    Counter = 0 
    while Counter < len(arrayQueryCategories): 
     print(str(Counter)) 
     try: 
      Result.append(arrayQueryCategories[Counter].index(Query)) 
      print("good") 
     except ValueError: 
      print("bad") 
      Result.append(-1) 
     Counter = Counter + 1 
    print(str(Result)) 
    defRespondTo() 

def defRespondTo(): 
    Responses = ["PlaceHolder-BrokenScreen","PlaceHolder-WetPhone"\ 
       "PlaceHolder-NotCharging","PlaceHolder-WontTurnOn"\ 
       "PlaceHolder-ButtonsBroken","PlaceHolder-Lockedout"] 
    Counter = 0 
    while Counter <= len(arrayQueryCategories): 
     if Result[Counter] != -1: 
      print(Responses[Counter]) 
      Counter = Counter + 1 
      defSearchFor() 


defSearchFor() 

結果:

Search Term: broken 
0 
good 
1 
bad 
2 
bad 
3 
bad 
4 
bad 
5 
bad 
[4, -1, -1, -1, -1, -1] 
PlaceHolder-BrokenScreen 
Search Term: lock 
0 
bad 
1 
bad 
2 
bad 
3 
bad 
4 
good 
5 
bad 
[-1, -1, -1, -1, 4, -1] 

有代碼和結果。在此之後,結果只是停止輸出,但沒有「>>>」表示過程已完成。爲什麼不重新發布?代碼應該無限循環。我的代碼有什麼問題?

回答

0

你的測試表明,沒有什麼不妥defSearchFor

所以看defRespondTo有環路存在,雖然Counter <=6循環,但Counter只遞增if內。

調試時,儘量減少儘可能多的代碼。您不需要任何導入,並且您知道第一個功能正在工作,因此請將其切除。這是我的程序的最小測試版本。它與您的bug有相同的錯誤,但更容易發現錯誤。

def defRespondTo(): 
    Counter = 0 
    while Counter <= 6: 
     if Result[Counter] != -1: 
      print(Counter) 
      Counter = Counter + 1  

Result = [-1,-1,-1,-1,4,-1] 

defRespondTo() 
+0

謝謝!這使我的任務變得更加簡單,謝謝! – SeanOTRS

+0

如果這有用,請考慮upvoting和接受。 –