First, complete the following class definition:

class BankAccount:
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()
这里用了一个列表来储存数据。其他没什么特别的。
class BankAccount:
def __init__(self, initial_balance):
self.balance = initial_balance
self.fee = [0] """Creates an account with the given balance.""" def deposit(self, amount):
"""Deposits the amount into the account."""
self.balance = self.balance + amount
return self.balance
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.
"""
self.balance = self.balance - amount
if self.balance < 0:
self.balance = self.balance - 5
self.fee[0] += 1 def get_balance(self):
"""Returns the current balance in the account."""
return self.balance
def get_fees(self):
"""Returns the total fees ever deducted from the account."""
return self.fee[0]*5

Quiz 6a Question 7————An Introduction to Interactive Programming in Python的更多相关文章

  1. Quiz 6b Question 8————An Introduction to Interactive Programming in Python

     Question 8 We can use loops to simulate natural processes over time. Write a program that calcula ...

  2. Quiz 6b Question 7————An Introduction to Interactive Programming in Python

     Question 7 Convert the following English description into code. Initialize n to be 1000. Initiali ...

  3. An Introduction to Interactive Programming in Python

    这是在coursera上面的一门学习pyhton的基础课程,由RICE的四位老师主讲.生动有趣,一共是9周的课程,每一周都会有一个小游戏,经历一遍,对编程会产生很大的兴趣. 所有的程序全部在老师开发的 ...

  4. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_3 练习

    Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...

  5. Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"

    Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...

  6. 【python】An Introduction to Interactive Programming in Python(week two)

    This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.e ...

  7. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_2 练习

    #Practice Exercises for Logic and Conditionals # Solve each of the practice exercises below. # 1.Wri ...

  8. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习

    # Practice Exercises for Functions # Solve each of the practice exercises below. # 1.Write a Python ...

  9. Mini-project # 4 - "Pong"___An Introduction to Interactive Programming in Python"RICE"

    Mini-project #4 - "Pong" In this project, we will build a version of Pong, one of the firs ...

随机推荐

  1. MySQL 设置数据库的隔离级别

    在会话级别设置隔离级别 1.read commited :set session transaction isolation level read committed; 2.repeatable re ...

  2. Apache配置支持include

    Apache配置支持include 什么是SSI? SSI是英文Server Side Includes的缩写,翻译成中文就是服务器端包含的意思.从技术角度上说,SSI就是HTML文件中,可以通过注释 ...

  3. cocos2dx mac环境搭建

    1)下载cocos2dx 2.2.3并解压

  4. sqlplus conn远程连接

    oracle.install.responseFileVersion=/oracle/install/rspfmt_dbinstall_response_schema_v11_2_0oracle.in ...

  5. ostringstream的使用方法

    ostringstream的使用方法 [本文来自]http://www.builder.com.cn/2003/0304/83250.shtml http://www.cppblog.com/alan ...

  6. Nhibernate初入门基本配置(二)

    转载地址http://www.cnblogs.com/kissdodog/p/3306428.html 使用NHibernate最重要的一步就是配置,如果连NHibernate都还没有跑的起来,谈何学 ...

  7. BootStrap 智能表单系列 五 表单依赖插件处理

    这一章比较简单哦,主要就是生产表单元素后的一些后续处理操作,比如日期插件的渲染.一些autocomplete的处理等,在回调里面处理就可以了, demo: $("input.date-pic ...

  8. objective-C学习笔记(九)ARC

    ARC叫自动引用计数Automatic Reference Counting.针对堆上的对象,管理对象的创建和释放. 哪些对象受ARC管理: OC对象指针 Block指针 使用_attribute_( ...

  9. spark学习

    大数据学习纲要,主要针对spark的相关知识的学习,后续会涉及其他大数据相关框架及语言. 1 spark入门: 2.spark 实战部分 3.spark源码解析

  10. php学习笔记(2)

    1.算数运算 <?php $a = 8; $b = 2; $c = 3; echo $a+$b."<br>\n"; echo $a-$b."<br ...