0
所以我有一個玩家輸入他/她的名字。該名稱被寫入一個文件。然後打開文件,讀取並將全局變量更改爲所述文件中的內容。這最終會成爲我爲一個班級開發的遊戲的一個保存功能。.txt閱讀以改變全球變數
def nameWrite():
text_file = open("name.txt", "w+")
print('what u name')
text_file.write(input())
text_file.close()
def nameRead():
text_file = open("name.txt","r")
print ("This is the output in file:",text_file.read())
global playerName
playerName = text_file.read()
text_file.close()
nameWrite()
nameRead()
print("You name is now:",playerName)
這爲什麼不改變變量playerName
?
最新錯誤? – smushi 2014-10-06 23:14:36
全局變量未更新 – 2014-10-06 23:16:39
代碼看起來像可以工作,有什麼問題?我不太喜歡使用'global';最好只是讓'nameRead()'函數'返回playerName'。 – 2014-10-06 23:18:58