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

2024-05-20 16:26:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)

在最后一行中,最后一个括号处有一个例外:“Parameter'file'unfilled。所以代码无法从我在最后一行中使用的函数中获取信息

如果有人能帮我,那就太好了!谢谢你


Tags: 代码nameselfaddforinputdefpassword
2条回答
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

这是我试图纠正你的代码,尽我所能。正如在注释中指出的,您需要将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

相关问题 更多 >