全局...未定义python [英] global ... is not defined python

查看:72
本文介绍了全局...未定义python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在python中逐个阅读一些单词,但出现此错误. "NameError:未定义全局名称'wordList'.

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:
    for line in f:
        for word in line.split():
              wordList[i]=word
              i+1
return wordList

推荐答案

您需要先定义wordList.而且您不能在空列表中随机分配索引.您可以轻松地用新值扩展"列表.

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

这篇关于全局...未定义python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆