2017-01-08 25 views
0

我試圖編寫一個簡單的遊戲,並需要在文件中寫入一些信息。這是怎樣的代碼看起來像至今:試圖使用類函數在文件中編寫字符串

class Player: 
    def __init__(self, name, password, file): 
     with open(file) as inputFile: 
      self.playerAndPw = inputFile.read() 
     self.name = name 
     self.password = password 


    def add(self, name, password, file): 
     file.write(name + " | " + password) 


    def __str__(self): 
     print("The player's name is called " + self.name + "\n") 

print("Welcome to Guess My Number!") 
start = input("Press 1 for New Account, 2 for Log In: ") 

if start == "1": 
    player = Player 
    playerID = input("Enter a name: ") 
    playerPassword = input("Enter a password: ") 
    fileName = "PlayerAndPassword.txt" 
    player.add(playerID, playerPassword, fileName) 

在最後一行出現在最後的支架異常:「參數‘文件’填充因此,代碼不能得到信息的功能我使用的最後一行。

將是巨大的,如果有人可以幫助我!謝謝!

+1

爲什麼你傳遞'名如果你已經將它們傳遞給了類構造函數,''''''''''''''''''作爲'Player.add'的參數嗎? – Tagc

+3

另外'player = Player'這一行並沒有初始化'Player'的新實例。要做到這一點,你需要'player = Player(...)'並傳遞所需的參數。 – Tagc

+1

但由於必須輸入文件,因此無法傳遞參數。看起來你的設計是有缺陷的:你不能用文件參數創建構造函數,因爲這意味着你永遠無法創建一個_new_播放器。更好地使用加載/保存方法,並使用json序列化/反序列化... –

回答

1

這是我試圖糾正你的代碼,盡我所能。正如指出的評論,您需要將player設置爲Player類的實例通過將其實例化爲player = Player(...)

因爲您傳遞播放器的名稱,密碼和文件以將憑據存儲到Player構造函數中,所以不需要將這些參數作爲參數傳遞給Player.add,這就是爲什麼我刪除所有參數的原因。

我應該指出,這個實現是非常簡單和不完整的,只是爲了解決你的即時問題而設計的。在每次調用Player構造函數後,我的實現將導致文件句柄保持打開狀態。如果您選擇這種方法,你可能需要閱讀the Python documentation on input and output operations.

class Player: 
    def __init__(self, name, password, fileName): 
     self.name = name 
     self.password = password 
     self.file = open(fileName, mode='a') 

    def add(self): 
     self.file.write(self.name + " | " + self.password + '\n') 

    def __str__(self): 
     print("The player's name is called " + self.name + "\n") 


print("Welcome to Guess My Number!") 
start = input("Press 1 for New Account, 2 for Log In: ") 

if start == "1": 
    playerId = input("Enter a name: ") 
    playerPassword = input("Enter a password: ") 
    fileName = "PlayerAndPassword.txt" 
    player = Player(playerId, playerPassword, fileName) 
    player.add() 

控制檯輸出

Welcome to Guess My Number! 
Press 1 for New Account, 2 for Log In: 1 
Enter a name: Tom 
Enter a password: Foo 

Welcome to Guess My Number! 
Press 1 for New Account, 2 for Log In: 1 
Enter a name: Dick 
Enter a password: Bar 

Welcome to Guess My Number! 
Press 1 for New Account, 2 for Log In: 1 
Enter a name: Harry 
Enter a password: Baz 

PlayersAndPasswords.txt

Tom | Foo 
Dick | Bar 
Harry | Baz 
+0

請注意,這種方法會留下一個打開的文件句柄,我會在這裏親自使用JSON。 JSON具有內置序列化的優點,並且使一些事情變得更容易(例如玩家查找)。 – MSeifert

+0

@MSeifert你沒有錯,我會將其添加到我的答案,因爲這是一個很好的觀點。但是,保留打開的文件句柄不會影響此程序的正確性。我的回答意在解決OP最直接的問題。 – Tagc

+0

@ErikaWorm很高興能有所幫助,但在堆棧溢出[投票和接受解決您的問題的答案是優先於'謝謝'意見。](http://stackoverflow.com/help/someone-answers) – Tagc

0
class Player: 
    def __init__(self, name, password, file): 
     self.name = name 
     self.password = password 
     self.file = open(file, mode='a') #first assign file to self.file(referring to this file) 


    def add(self): #need to add those parameters as they are already initialized by constructor 
     self.file.write(self.name + " | " + self.password) 


    def __str__(self): 
     print("The player's name is called " + self.name + "\n") 

print("Welcome to Guess My Number!") 
start = input("Press 1 for New Account, 2 for Log In: ") 

if start == "1": 
    playerID = input("Enter a name: ") 
    playerPassword = input("Enter a password: ") 
    fileName = "PlayerAndPassword.txt" 
    player = Player(playerID, playerPassword, fileName) #create instance with said values 
    player.add() #call the add function to add 
+0

是的,我使用了評論。 – Inconnu

+0

啊,是的,檢查它,現在它工作。 – Inconnu

相關問題