python写作业题_一个python作业题,关于类的书写

class BankAccount:

""" Class definition modeling the behavior of a simple bank account """

def __init__(self, initial_balance):

"""Creates an account with the given balance."""

def deposit(self, amount):

"""Deposits the amount into the account."""

def withdraw(self, amount):

"""

Withdraws the amount from the account. Each withdrawal resulting in a

negative balance also deducts a penalty fee of 5 dollars from the balance.

"""

def get_balance(self):

"""Returns the current balance in the account."""

def get_fees(self):

"""Returns the total fees ever deducted from the account."""

The deposit and withdraw methods each change the account balance. The withdraw method also deducts a fee of 5 dollars from the balance if the withdrawal (before any fees) results in a negative balance. Since we also have the method get_fees, you will need to have a variable to keep track of the fees paid.

Here's one possible test of the class. It should print the values 10 and 5, respectively, since the withdrawal incurs a fee of 5 dollars.

my_account = BankAccount(10)

my_account.withdraw(15)

my_account.deposit(20)

print my_account.get_balance(), my_account.get_fees()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值