global … is not defined python

烂漫一生 提交于 2019-12-02 13:25:47

You need to define wordList to begin with. And you cannot randomly assign indexes in an empty list. You can easily 'extend' the list with new values.

worldList = []
with fitxer as f:
    for line in f:
        wordList.extend(line.split())
return wordList

wordList is not instantiated as a list or not in scope.

If wordList is a global variable, the beginning of your function will need global wordList Not needed because the object is mutated

If the list should only be in scope of the function you will need to instantiate it as a list.

wordList = []

Edit: as deceze pointed out

Within the function itself you should be appending to the list since the index does not exists.

wordList.append(word)

You haven't defined wordList before you try to use it in your loop or return it.

Try adding wordList = [] below i=0 to declare wordList as an empty list.

Also, you should use wordList.append(i) to add your word this list.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!