我在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()
你可以帮我解决这个问题吗?
答案 0 :(得分:2)
您的变量D
是函数main
的本地变量,当然,外部代码看不到它(您甚至在运行之前尝试访问它 {{ 1}})。做点什么
main