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. Effective C++学习笔记——构造/析构/拷贝运算

    条款9:决不再构造和析构过程中调用virtual函数,包括通过函数间接调用virtual函数. 应用:想在一个继承体系中,一个derived class被创建时,某个调用(例如生成相应的日志log)会 ...

  2. 转: Nodejs 发送HTTP POST请求实例

    项目里面需要用到使用NodeJs来转发HTTP POST请求,把过程记录一下: exports.sendEmail = function (req, res) { res.send(200, req. ...

  3. USB創意讀卡機設計特別獎

  4. 三种尺寸:手机SIM卡使用指南

    毫无疑问目前卖的最火的手机非iPhone 5s莫属,相信仍有不少网友目前处于观望之中,由于iPhone 5s和iPhone 5c采用与iPhone相同的Nano-SIM卡,因此不少新用户在使用之前也徒 ...

  5. Qt信息隐藏(Q_D/Q_Q)介绍——从二进制兼容讲起

    http://www.cnblogs.com/SkylineSoft/articles/2046404.html

  6. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)

    [题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...

  7. Static block start new thread

    Static block start new thread public class StaticThreadInit { static{ Threadt = newThread(){ public ...

  8. C++中搜索、截取字符串

    演示样例中有具体凝视,直接上代码: #include <iostream> #include <string> using std::cout; using std::endl ...

  9. js兼容性大全

    js有个第二定律好的属性/选择器一定不兼容/* 获取类名通用代码*/function getClassName(){ if(document.getElementsByClassName){ doso ...

  10. 虎说:bootstrap源码解读(重置模块)

    ------<!--action-->------ 开场show:前不生“不犹豫”,后半生“不后悔”.今天又逃课,我不后悔 素材:推特公司的前端框架bootstrap(下称bt),解读源码 ...