Homework 11
Due at 11:59:59 pm on Saturday, 11/30/2019.
Instructions
Download hw11.zip. Inside the archive, you will find starter files for the questions in this homework, along with a copy of the OK autograder.
Submission: When you are done, submit with python3 ok --submit
. You may submit more than once before the deadline; only the final submission will be scored. Check that you have successfully submitted your code on okpy.org. See this article for more instructions on okpy and submitting assignments.
Readings: This homework relies on following references:
Question 1: Account
There are several things wrong with the following code! Debug the Account
class to satisfy the docstring.
class Account(object):
"""A bank account that allows deposits and withdrawals.
>>> sophia_account = Account('Sophia')
>>> sophia_account.deposit(1000000) # depositing my paycheck for the week
1000000
>>> sophia_account.transactions
[('deposit', 1000000)]
>>> sophia_account.withdraw(100) # buying dinner
999900
>>> sophia_account.transactions
[('deposit', 1000000), ('withdraw', 100)]
"""
interest = 0.02
balance = 1000
def __init__(self, account_holder):
self.balance = 0
self.holder = account_holder
self.transactions = []
def deposit(self, amount):
"""Increase the account balance by amount and return the
new balance.
"""
self.transactions.append(('deposit', amount))
Account.balance = self.balance + amount
return self.balance
def withdraw(self, amount):
"""Decrease the account balance by amount and return the
new balance.
"""
self.transactions.append(('withdraw', amount))
if amount > self.balance:
return 'Insufficient funds'
self.balance = Account.balance - amount
return Account.balance
Use OK to test your code:
python3 ok -q Account
Question 2: Checking account
We'd like to be able to cash checks, so let's add a deposit_check
method to our CheckingAccount
class. It will take a Check
object
as an argument, and check to see if the payable_to
attribute matches
the CheckingAccount
's holder. If so, it marks the Check
as
deposited, and adds the amount specified to the CheckingAccount
's
total.
Write an appropriate Check
class, and add the deposit_check
method
to the CheckingAccount
class. Make sure not to copy and paste code!
Use inheritance whenever possible.
See the doctests for examples of how this code should work.
class CheckingAccount(Account):
"""A bank account that charges for withdrawals.
>>> check = Check("Steven", 42) # 42 dollars, payable to Steven
>>> steven_account = CheckingAccount("Steven")
>>> eric_account = CheckingAccount("Eric")
>>> eric_account.deposit_check(check) # trying to steal steven's money
The police have been notified.
>>> eric_account.balance
0
>>> check.deposited
False
>>> steven_account.balance
0
>>> steven_account.deposit_check(check)
42
>>> check.deposited
True
>>> steven_account.deposit_check(check) # can't cash check twice
The police have been notified.
"""
withdraw_fee = 1
interest = 0.01
def withdraw(self, amount):
return Account.withdraw(self, amount + self.withdraw_fee)
class Check(object):
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q CheckingAccount
Question 3: Errors
It is often said that nothing in life is certain but death and taxes. For a programmer or data scientist, however, nothing is certain but encountering errors.
In Python, there are two primary types of errors, both of which you are likely familiar with: syntax errors and exceptions. Syntax errors occur when the proper structure of the language is not followed, while exceptions are errors that occur during the execution of a program. These include errors such as ZeroDivisionError, TypeError, NameError, and many more!
Under the hood, these errors are based in the concepts of object orientation, and all exceptions are class objects. If you're interested in more detailed explanations of the structure of exceptions as well as how to create your own, check out this article from the Python documentation! In the meantime, we'll implement our own version of an Error
class
Complete the Error
, SyntaxError
, and ZeroDivisionError
classes such that
they create the correct messages when called.
- The
SyntaxError
andZeroDivisionError
classes inherit from theError
class and add functionality that is unique to those particular errors. Their code is partially implemented for you. - The
add_code
method adds a new helpful message to your error, while thewrite
method should print the output that you see when an error is raised. - You can access the parent class methods using the super() function
class Error:
"""
>>> err1 = Error(12, "error.py")
>>> err1.write()
'error.py:12'
"""
def __init__(self, line, file):
"*** YOUR CODE HERE ***"
def write(self):
return self.file + ':' + str(self.line)
class SyntaxError(Error):
"""
>>> err1 = SyntaxError(17, "HW10.py")
>>> err1.write()
HW10.py:17 SyntaxError : Invalid syntax
>>> err1.add_code(4, "EOL while scanning string literal")
>>> err2 = SyntaxError(18, "HW10.py", 4)
>>> err2.write()
HW10.py:18 SyntaxError : EOL while scanning string literal
"""
type = 'SyntaxError'
msgs = {0 : "Invalid syntax", 1: "Unmatched parentheses", 2: "Incorrect indentation", 3: "missing colon"}
def __init__(self, line, file, code=0):
"*** YOUR CODE HERE ***"
def write(self):
end = self.type + ' : ' + self.message
"*** YOUR CODE HERE ***"
def add_code(self, code, msg):
"*** YOUR CODE HERE ***"
class ZeroDivisionError(Error):
"""
>>> err1 = ZeroDivisionError(273, "HW10.py")
>>> err1.write()
HW10.py:273 ZeroDivisionError : division by zero
"""
type = 'ZeroDivisionError'
def __init__(self, line, file, message='division by zero'):
"*** YOUR CODE HERE ***"
def write(self):
end = self.type + ' : ' + self.message
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q Error
Use OK to test your code:
python3 ok -q SyntaxError
Use OK to test your code:
python3 ok -q ZeroDivisionError
Question 4: Project
Nothing to submit for this question, just a reminder to work on your project! Good luck!