global … is not defined python

后端 未结 3 1332
有刺的猬
有刺的猬 2021-01-29 12:24

I need to read some words text by text in python, and I am getting this error. \"NameError: global name \'wordList\' is not defined.

i=0
with fitxer as f:
    fo         


        
3条回答
  •  -上瘾入骨i
    2021-01-29 12:52

    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
    

提交回复
热议问题