Account
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Background
Here we define a new data type (a class) representing a simple bank account.
class Account:
def __init__(self):
self.balance = 0
"Account" is the name of the class. Recall that "self" refers to the object being created (here, the particular account).
We can make an object of this class (a bank account) and then add money to the account:
>>> checking = Account()
>>> checking.balance
0
>>> checking.balance += 10
>>> checking.balance
10
But, it is considered bad style to manipulate the internal state manually like this. Instead, we'll augment the class with a procedure (a "method") that knows how to make a deposit:
class Account:
def __init__(self):
self.balance = 0
def deposit(self, n):
self.balance += n
With this new class definition, we can make an account and deposit to it in a few different ways:
>>> checking = Account()
>>> checking.balance
0
>>> Account.deposit(checking, 20)
>>> checking.balance
20
>>> checking.deposit(15)
>>> checking.balance
35
We can also make another account:
>>> savings = Account()
>>> savings.balance
0
>>> savings.deposit(60)
>>> savings.balance
60
>>> checking.balance
35
1) Withdrawals
We want the Account class to allow withdrawals, so that you can do the following:
>>> savings.withdrawal(5)
>>> savings.balance
55
Download the starter code file and augment the Account
class to support withdrawal. These readings on methods might be useful!
2) Currency Conversion
Now we want to augment the Account
class in two more ways. You should type both of these augmentations into the same file you submitted previously.
First, you want to know what your balance is, but in Euros. Use the conversion factor 1 Euro = $1.12. Round to the nearest Euro penny (documentation for Python's round function may be helpful). For example your accounts should support:
>>> savings.balance
55
>>> savings.how_much_in_euros()
49.11
Notice that how_much_in_euros
doesn't have any input arguments, but when defining the method, you'll still want to include the self
argument. You can then use the self
variable to access the current balance.
Second, you want to be able to specify an initial balance for the account. You tried to implement this functionality like so:
class Account:
def __init__(self, initial_balance):
self.balance = initial_balance
def deposit(self, n):
self.balance += n
# ...
Now, you can indeed create an account as follows!
>>> checking = Account(12)
>>> checking.balance
12
However, what happens when you try to use the previous argumentless syntax, checking = Account()
, to create an instance of Account
?
Modify the definition of __init__
so that this previous syntax still also
works, and just makes the initial balance 0. Note that this should be a minor modification. You may wish to review this week's "Extra Goodies" readings.
Submit your updated definition of the Account
class below. It should include the withdrawal
function, how_much_in_euros
function, and the ability to support 0 or 1 arguments to __init__
.
Next Exercise: Vector