python名称错误名称未定义

时间:2013-10-10 00:22:46

标签: python python-3.x

我在python3中运行此代码时得到的错误名称未定义:

def main():
    D = {} #create empty dictionary
    for x in open('wvtc_data.txt'):
        key, name, email, record = x.strip().split(':')
        key = int(key) #convert key from string to integer
        D[key] = {} #initialize key value with empty dictionary
        D[key]['name'] = name
        D[key]['email'] = email
        D[key]['record'] = record

print(D[106]['name'])
print(D[110]['email'])
main()

你可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

您的变量D是函数main的本地变量,当然,外部代码看不到它(您甚至在运行之前尝试访问它 {{ 1}})。做点什么

main