我在处理这段代码时遇到了问题。尽管名称错误似乎很普遍,但我无法通过搜索找到解决方法。这是代码...
def fmp_sel():
with open ('MonPlotDb.csv', 'rU') as csvfile:
next(csvfile, None)
fmpList = csv.reader(csvfile, delimiter=',', dialect=csv.excel_tab)
for item in enumerate(fmpList):
print "[%d] %s" % (item)
while True:
try:
in_Sel = raw_input(('''Choose from list or 'q' to quit:'''))
if in_Sel == 'q':
print 'Quit?'
conf = raw_input('Really? (y or n)...')
if conf == 'y':
print 'Seeya!'
break
else:
continue
plotOrig = DataPlotLoc[int(in_Sel) + 1]
print 'You selected', plotOrig[1]
break
except (ValueError, IndexError):
print 'Error: Try again'
和追溯....
File "E:\FireRegDb\Rec_2012\dblist_outonly.py", line 28, in fmp_sel
plotOrig = DataPlotLoc[int(in_Sel) + 1]
NameError: global name 'DataPlotLoc' is not defined
这个函数是从 main() 调用的,但我不明白为什么“DataPlotLoc”是一个全局名称,因为它在这个函数中。无论哪种方式,我认为我缺少一条线来定义它,但是如何以及在哪里,我不知道。我很想得到一些帮助。
编辑:只是为了添加更多信息..'DataPlotLoc'是列表插入代码时的名称,即。DataPlotLoc=[['a', 'b', 'c',....]] 并且它起作用了。线 plotOrig = DataPlotLoc[int(in_Sel) + 1] 指的是这个列表,但显然它现在正在被 csv.reader 读入,所以现在我不确定如何分配这个变量。我假设在确认用户是否输入“q”之后,我仍然需要它接受一个整数,并且 +1 是添加到输入的数字上,以便它与从列表中选择的相应行项目的正确索引号对齐。对不起,如果这有点令人困惑,但我自己有点困惑......