我在這裏有一個簡單的問題。我有一個txt文件中的兩個數字,我正在嘗試創建一個方法,可以在我的代碼中爲txt文件中的這兩個數字更改兩個變量。在Python中加載一個txt文件
這裏是我的方法:
def loadCoords(cordX, cordY):
i=0;
f1 = open('continue.txt', "r")
f2 = open('continue.txt', "r")
f1.readline();
while i<2:
f2.readline();
i=i+1;
#already tested>>> cord=f.readline() xD
#also tried to put another names for the cordX and cordY here inside xD
cordY=f2;
cordX=f1;
return cordX;
return cordY;
這裏是我打電話的方法:
if evento.type == pygame.KEYDOWN:
if evento.key == pygame.K_a:
print('GAME BEGIN')
GAME_BEGIN = True
loadCoords(cordX,cordY);
有人可以幫助?
什麼是你所得到的錯誤? –
沒有人,它只是無法讀取f1和f2文件,coordX和Y打印0和0,我已經在開始聲明的數字。 – Aury0n
可能的重複[如何在Python中將文本文件讀入字符串變量](http://stackoverflow.com/questions/8369219/how-do-i-read-a-text-file-into-a-字符串變量在Python中) – DeepSpace