0
我在我的Python代码和其他几个函数中有一个主函数。在我的主要中,我访问了另一个创建字典的函数。在我的Python代码的末尾是一个将文本写入文本文件的if语句。我无法弄清楚如何访问从前面的函数创建的字典。将字典传递给其他函数
这里是我的代码目前是如何工作的
def main:
# "does something"
call function X
# does other stuff
def X:
#create dictionary
dict = {'item1': 1,'item2': 2}
return dictionary
....
.... # other functions
....
if __name__ == "__main__":
# here is where I want to write into my text file
f = open('test.txt','w+')
main()
f.write('line 1: ' + dict[item1])
f.write('line 2: ' + dict[item2])
f.close()
我刚开始学习Python所以任何帮助是非常赞赏的典范!谢谢!