非常新的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
registerUsername和registerPassword看起来都需要两次用户名和密码。有很多方法可以解释代码和试图做什么。如果你发布更多的问题,那么我可以进一步帮助你
编辑: 感谢您上传您尝试的图像
图像中的最后一行,替换为:
这将调用
__init__
函数并创建一个1000美元的银行帐户,用户名为Adam,密码为password。接下来,添加以下行:这将从余额中取出250美元,然后打印帐户的当前余额,现在是750美元
我们所做的是创建一个BankAccount对象并将其存储到
account
变量中。如果我们想创建另一个BankAccount对象,我们可以,这两个帐户将是分开的。例如:希望这有帮助。坚持下去
相关问题 更多 >
编程相关推荐