请设计一个account类表示账户,自行设计该类中的属性和方法,并利用这个类创建一个账号为998866,余额为2000,年利率为4.5%的账户,然后从该账户中存入150,取出1500,打印出账号,余额,年利率,月利息,月息
时间: 2024-06-08 12:06:50 浏览: 37
设计一个银行账户类。其中包括以下内容,并用字符界面模拟存款和取款过程。
5星 · 资源好评率100%
以下是一个简单的账户类的实现:
```python
class Account:
def __init__(self, account_number, balance, annual_interest_rate):
self.account_number = account_number
self.balance = balance
self.annual_interest_rate = annual_interest_rate
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance - amount >= 0:
self.balance -= amount
else:
print("Insufficient funds")
def monthly_interest(self):
monthly_interest_rate = self.annual_interest_rate / 12
return self.balance * monthly_interest_rate
def monthly_balance(self):
return self.balance / 12
```
然后我们可以创建一个账户对象:
```python
account = Account('998866', 2000, 0.045)
```
接下来我们可以从账户中存入150:
```python
account.deposit(150)
```
然后从账户中取出1500:
```python
account.withdraw(1500)
```
最后,我们可以打印出账户信息:
```python
print(f"Account Number: {account.account_number}")
print(f"Balance: {account.balance}")
print(f"Annual Interest Rate: {account.annual_interest_rate}")
print(f"Monthly Interest: {account.monthly_interest()}")
print(f"Monthly Balance: {account.monthly_balance()}")
```
输出结果如下:
```
Account Number: 998866
Balance: 650
Annual Interest Rate: 0.045
Monthly Interest: 2.4375
Monthly Balance: 54.166666666666664
```
其中,“月息”可以通过调用 `monthly_interest()` 方法来计算。
阅读全文