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所以任何幫助是非常讚賞的典範!謝謝!