尝试使用类函数在文件中编写字符串

时间:2017-01-08 14:52:51

标签: python function class

我试图写一个简单的游戏,需要在文件中写一些信息。这就是代码到目前为止的样子:

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)

在最后一行中,最后一个括号有一个例外:"参数'文件'填补。因此,代码无法从我在最后一行中使用的函数中获取信息。

如果有人可以帮助我,那会很棒!谢谢!

2 个答案:

答案 0 :(得分:1)

这是我尽力纠正您的代码的尝试。正如评论中所指出的,您需要通过将player实例化为Player来将player = Player(...)设置为Player类的实例。

因为您传递了播放器的名称,密码和用于将凭据存储到Player.add构造函数的文件,所以您不需要将这些作为参数传递给{{ 1}},这就是我删除所有参数的原因。

我应该指出,此实施非常简单且不完整,旨在解决您的直接问题。在每次调用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

答案 1 :(得分: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