为什么我的register函数不能在Python中工作

2024-10-01 02:23:02 发布

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

非常新的Python程序员,我的第一个OOP项目。我开了一个银行账户。大胆,我知道。以下是我目前的代码:


    def __init__(self, balance, username, password):
        """ Making the actual bank account :) """
        self.money = balance

    def registerUsername(self, username):
        self.username = username
        self.username = input("What is your username? ")

    def registerPassword(self, password):
        self.password = password
        self.password = input("What is your password? ")

    def login(self, attemptedUsername, attemptedPassword):
        self.attemptedUsername = attemptedUsername
        self.attemptedPassword = attemptedPassword
        if self.attemptedUsername == self.username and self.attemptedPassword == self.password:
            print("Success! You have been logged in! ")
        elif self.attemptedUsername != self.username and self.attemptedPassword != self.password:
            print('None of the fields entered are correct. Please try again! ')
        elif self.attemptedUsername == self.username and self.attemptedPassword != self.password:
            print("Your password is incorrect. Please try again! ")
        elif self.attemptedUsername != self.username and self.attemptedPassword == self.password:
            print("Your username is incorrect. Please try again! ")

    def deposit(self, amount):
        """ Deposit money using this function! """
        self.money += amount
        return self.money

    def withdraw(self, amount):
        """ Withdraw money using this method! """
        self.money -= amount
        return self.money

    def getbalance(self):
        """ Returns the amount of money you have in your account """
        return self.money

所以,我的寄存器函数不断中断。密码和用户名。当我使用它时,它要求我在写入用户和密码之前写入一个Self: BankAccount参数。请帮忙

编辑:

这是我的问题。我附上了一张照片

如您所见,它让我在编写实际用户名之前输入一个self参数。我不想在username之前输入任何参数,有没有办法消除self


Tags: andtheselfyourisdefusernamepassword
1条回答
网友
1楼 · 发布于 2024-10-01 02:23:02

registerUsername和registerPassword看起来都需要两次用户名和密码。有很多方法可以解释代码和试图做什么。如果你发布更多的问题,那么我可以进一步帮助你

编辑: 感谢您上传您尝试的图像

图像中的最后一行,替换为:

account = BankAccount(1000, 'Adam', 'Password')

这将调用__init__函数并创建一个1000美元的银行帐户,用户名为Adam,密码为password。接下来,添加以下行:

account.withdraw(250)
print(account.getbalance())

这将从余额中取出250美元,然后打印帐户的当前余额,现在是750美元

我们所做的是创建一个BankAccount对象并将其存储到account变量中。如果我们想创建另一个BankAccount对象,我们可以,这两个帐户将是分开的。例如:

account_one = BankAccount(1000, 'Adam', 'Password')
account_two = BankAccount(1000, 'Ben', 'AnotherPassword')

account_one.withdraw(250)
print(account_one.getbalance())

account_two.deposit(500)
print(account_two.getbalance())

# Account One has $750. Account Two has $1500

希望这有帮助。坚持下去

相关问题 更多 >