为什么我的寄存器函数在Python中不工作



这是我的第一个面向对象项目。我开了一个银行账户。大胆,我知道。下面是我到目前为止的代码:


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参数。我不想在用户名之前输入任何参数,有办法消除自我吗?

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

希望这对你有帮助。坚持下去。

最新更新