Lab, Practical Projects
7 tasks, easy to hard: Predict, then run, Write it, Complete it, Fix it, Trace it, Refactor it, Mini challenge. Hints come one at a time and you can see each answer. Lab work is saved as practice and earns no XP.
Open the lessonYou can read every task below. Running, checking and saving them needs a free ID.
7 tasks
Task 01 .Type what the program will print, then run it to compare.
Type exactly what this program prints. The current account overrides only available.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 02 .Solve it from the prompt, starting from an empty editor.
Write a class Account with owner, a balance starting at 0 and an empty history list. Its deposit(amount) raises ValueError for amounts that are not positive, otherwise adds the amount and appends ("deposit", amount, new_balance). Deposit 40 and 2.5 for Ana, then print the balance and the history.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 03 .Fill the marked gaps in the starter code.
Replace ___ so InsufficientFundsError belongs to the BankError family and is caught by the general except.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 04 .The starter code has a bug. Repair it so the checks pass.
A refused withdrawal must leave the balance unchanged, so the program should print refused and then 20. Right now the balance is changed before the check. Fix withdraw.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 05 .Fill the table with the variable values, step by step.
A savings account allows two withdrawals. Each row shows taken and refused each time one of them changes, once both exist.
class Savings:
def __init__(self, balance):
self.balance = balance
self.count = 0
def withdraw(self, amount):
if self.count >= 2 or amount > self.balance:
raise ValueError("refused")
self.balance -= amount
self.count += 1
s = Savings(100)
taken = 0
refused = 0
for amount in [30, 90, 20, 10]:
try:
s.withdraw(amount)
taken = taken + amount
except ValueError:
refused = refused + 1Sign in or create a free ID to fill in and check this table.
Task 06 .Rewrite working code to the stated rule, keeping the same output. Rule: Account.withdraw uses self.available(); Current overrides only available and has no withdraw of its own.
This code works, but Current copies the whole withdraw method just to change the limit. Add an available() method to Account, use it inside withdraw, and make Current override only available. The output must not change.
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.
Task 07 .A short task that combines this lesson with earlier ones.
Write BankError and InsufficientFundsError(BankError), and an Account(owner, balance=0) with deposit, withdraw (raising InsufficientFundsError with the message OWNER has BALANCE, balance shown with two decimals) and a __str__ returning OWNER: BALANCE with two decimals. Write transfer(source, target, amount) so a refused transfer changes nothing. Run the starter steps so the output is:
Ana: 70.00 Ben: 80.00 refused: Ben has 80.00 Ana: 70.00 Ben: 80.00
Sign in to access the interactive Python editor, write and run code checks against automated tests, use hints, and save your practice. Practice does not award XP.