为什么我在调用main()时出错?

时间:2016-11-05 01:15:13

标签: python

所以这里有一些代码我应该用来输入用户输入的文字并创建一个字典。我可以告诉我为什么在调用函数main()时会出现回溯错误吗?

 def build_index(text):
    index = {}
    words = text.split()

    position = 0

    for x in text:
        if x.isalpha() == False and x.isdigit() == False:
            text.join(x)
        else:
            text.replace(x,'')

    while position < len(words):
        nextword = words[position]

        if nextword in index:
            ref = index[nextword]
            ref.append(position)
            index[nextword] = ref

        else:
            list = []
            list.append(position)
            index[nextword] = list
        position += 1


def displayindex(index):
    keys = sorted(index.keys())
    for key in keys:
        print(key + ':' + str(index[key]))

def main():
    text = input("enter text")
    build_index(text)
    displayindex(index)
main()

2 个答案:

答案 0 :(得分:0)

通过将input更改为raw_input(对于Python 2.7),我能够摆脱回溯错误。但是您有其他错误,例如方法index中的main未定义。以下作品:

index = {}

def build_index(text):
    global index
    words = text.split()

    position = 0

    for x in text:
        if x.isalpha() == False and x.isdigit() == False:
            text.join(x)
        else:
            text.replace(x,'')

    while position < len(words):
        nextword = words[position]

        if nextword in index:
            ref = index[nextword]
            ref.append(position)
            index[nextword] = ref

        else:
            list = []
            list.append(position)
            index[nextword] = list
        position += 1


def displayindex(index):
    keys = sorted(index.keys())
    for key in keys:
        print(key + ':' + str(index[key]))

def main():
    global index
    text = raw_input("enter text")
    build_index(text)
    displayindex(index)
main()

答案 1 :(得分:0)

回溯错误内容取决于您运行代码的Python版本。在Python 3.x中,回溯解释了它产生错误的原因:

  

追踪(最近的呼叫最后):
    文件&#34; ./ prog.py&#34;,第37行,在     文件&#34; ./ prog.py&#34;,第36行,在主要文件中   NameError:name&#39; index&#39;未定义

TLDR:需要添加/更改3行代码。请参阅以下代码中的评论

NameError告诉我们,它不知道名称index所指的是什么,因为它超出了main方法的范围,并且没有#{1}}方法的范围。尚未定义。您可以创建global变量的index实例,如MeterLongCat的答案中所述,但是因为索引 已创建和定义当我们调用build_index时,我们可以在该方法调用之后返回index,保存其返回值,然后将其传递给displayindex函数,如下所示。

OTOH,在Python 2中,正如MeterLongCat所指出的那样,你想要从用户那里得到一个字符串,而不是input的字符串,你想要raw_input

def build_index(text):
    index = {}
    words = text.split()

    position = 0

    for x in text:
        if x.isalpha() == False and x.isdigit() == False:
            text.join(x)
        else:
            text.replace(x,'')

    while position < len(words):
        nextword = words[position]

        if nextword in index:
            ref = index[nextword]
            ref.append(position)
            index[nextword] = ref

        else:
            list = []
            list.append(position)
            index[nextword] = list
        position += 1
    return index                    # Return the index


def displayindex(index):
    keys = sorted(index.keys())
    for key in keys:
        print(key + ':' + str(index[key]))

def main():
    text = raw_input("enter text")  # Use raw_input
    index = build_index(text)       # Assign the index
    displayindex(index)
main()