Lab, Classes and Objects
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. Each object keeps its own attributes.
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 Book whose __init__ stores title and pages, with a method reading_time() that returns the minutes to read it at 2 pages per minute. Print the title and reading time of Book("Dune", 412) as Dune 206.0.
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 grow increases the plant's height by the given amount.
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.
The program should print Lamp is on. Two mistakes stop it: a method without self, and a value stored in a local variable instead of an attribute. Fix both.
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.
The program copies an attribute into a variable after each click. Each row shows presses and total each time one of them changes, once both exist.
class Clicker:
def __init__(self):
self.count = 0
def click(self, times):
self.count += times
c = Clicker()
total = 0
for presses in [2, 1, 3]:
c.click(presses)
total = c.countSign 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: Define class Timer with add and show methods; no dictionary for the timer.
This code works but keeps the timer in a dictionary with separate functions. Rewrite it as a class Timer with __init__, add(seconds) and show() methods, and no dictionary. 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 a class Basket that starts empty and has add(item, price, qty=1), remove(item) (removes every line with that item), total() and count() (the number of items, counting quantities). Run the steps in the starter so the output is:
5 items, total 14.5 2 items, total 13.0
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.