GCSE实践的Python错误

时间:2016-09-14 17:46:41

标签: python-3.x

代码:

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]

有代码和结果。在那之后结果只是停止输出,但是没有“&gt;&gt;&gt;”表示该过程已完成。为什么不重新开口?代码应无限循环。我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

您的测试显示defSearchFor

没有任何问题

所以请看defRespondTo那里有一个循环,在Counter <=6时循环,但Counter仅在if内递增。

调试时,尽量减少尽可能多的代码。您不需要任何导入,并且您知道第一个功能正在运行,因此请将其删除。这是我的程序的最小测试版本。它与您的错误相同,但更容易发现错误。

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

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

defRespondTo()