[英]Transaction History using Python
First of all, I created a bank account for withdrawing money, depositing, and seeing the balance and now I want to add transaction history when users add money or withdraw here's what I've tried but I didn't succeed.首先,我创建了一个用于取款、存款和查看余额的银行账户,现在我想在用户添加或取款时添加交易历史记录这是我尝试过但没有成功的方法。 Any help?
有什么帮助吗?
class BankAccount:
def __init__(self,name,accountNumber,salary):
self.name = name
self.accountNumber = accountNumber
self.salary = salary
list = []
def description(self):
print("Name is: " , self.name)
print("AccountNumber is: " , self.accountNumber)
print("Salay is: " , self.salary)
def deposit(self,deposit):
self.salary = self.salary + deposit
list.append(deposit)
def withdraw(self,withdraw):
self.salary = self.salary - withdraw
list.append(withdraw)
def transaction_history(self,withdraw,deposit):
list.append(deposit)
list.append(withdraw)
bank = BankAccount("John" , 420402 , 5000)
bank.deposit(500)
It's not a good practice to declare a variable which is already the name of a class in Python. That being said, you can declare two new variables self.deposit_history
and self.withdraw_history
in the __init__
function as:在 Python 中声明一个已经是 class 名称的变量不是一个好习惯。也就是说,您可以在
__init__
function 中声明两个新变量self.deposit_history
和self.withdraw_history
为:
def __init__(self,name,accountNumber,salary):
self.name = name
self.accountNumber = accountNumber
self.salary = salary
self.deposit_history = []
self.withdraw_history = []
and append these lists in respective functions as:和 append 这些列表在各自的功能中为:
def deposit(self,deposit):
self.salary = self.salary + deposit
self.deposit_history.append(deposit)
def withdraw(self,withdraw):
self.salary = self.salary - withdraw
self.withdraw_history.append(withdraw)
and finally print them in transaction_history()
as:最后在
transaction_history()
中将它们打印为:
def transaction_history(self):
print("Deposits: ", self.deposit_history)
print("Withdraws: ", self.withdraw_history)
Output: Output:
>>> bank = BankAccount("John" , 420402 , 5000)
>>> bank.deposit(500)
>>> bank.withdraw(300)
>>> bank.transaction_history()
Deposits: [500]
Withdraws: [300]
>>> bank.description()
Name is: John
AccountNumber is: 420402
Salary is: 5200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.