Quiz 6a Question 7————An Introduction to Interactive Programming in Python
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的更多相关文章
- 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 ...
- 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 ...
- An Introduction to Interactive Programming in Python
这是在coursera上面的一门学习pyhton的基础课程,由RICE的四位老师主讲.生动有趣,一共是9周的课程,每一周都会有一个小游戏,经历一遍,对编程会产生很大的兴趣. 所有的程序全部在老师开发的 ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- html字符实体对照表
- [VC6 console]调用API获取手机归属地
为了完成作业,就偷个懒糊了个获取手机归属地的程序,.我原本写的是MFC版本的,但是由于MFC的代码不是很通用,加上我没有学MFC的时候看别人MFC代码只能干瞪眼,看不懂,所以便改成控制台版本的了.但这 ...
- POJ 1721 CARDS(置换群)
[题目链接] http://poj.org/problem?id=1721 [题目大意] 给出a[i]=a[a[i]]变换s次后的序列,求原序列 [题解] 置换存在循环节,因此我们先求出循环节长度,置 ...
- Java图形化界面设计——布局管理器之CardLayout(卡片布局)
- JavaScript中你可能不知道的九件事
今天凑巧去W3School扫了一遍JavaScript教程,发现从中看到了不少自己曾经没有注意过的细节. 我这些细节列在这里.分享给可能相同不知道的朋友: 1.使用 document.write() ...
- Open virtual effects in Ubuntu 12.04LTS
Need install below packages: compiz compiz-core compiz-fusion-plugins-extra+ compiz-fusion-plugins-m ...
- 关于js封装框架类库之样式操作
在js中,对样式的操作我们并不感到陌生,在很多框架中都是用极少的代码,实现更强大的功能,在这做出一些的总结.存在不足还望指出! 1.封装一个添加css的方法(这篇引用了前面的框架结构) 在 js 中 ...
- java 錯誤集錦.
(1)加载驱动成功com.microsoft.sqlserver.jdbc.SQLServerException: 不支持此服务器版本.目标服务器必须是 SQL Server 2000 或更高版本.链 ...
- Spring—Hibernate
1.家jar包 2配置applicationContext与xxx.hbm.xml(根据需要决定是否配置hibernate.hbm.xml) applicationContext.xml <?x ...
- cocos2d-x3.9利用cocos引擎一键打包Android平台APK(C++小白教程)
链接地址:http://www.cocoachina.com/bbs/read.php?tid=333937 cocos2d-x3.9利用cocos引擎一键打包Android平台APK(C++小白教程 ...