Lab, Classes in Depth
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.
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 Rectangle(width, height) with an area() method, a class method square(side) that builds a rectangle with equal sides, and a static method is_valid(width, height) that returns True when both are positive. Print Rectangle.square(3).area(), Rectangle(2, 5).area(), Rectangle.is_valid(2, 0) and Rectangle.is_valid(1, 1).
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 ___ with the decorator that makes from_pair receive the class, so the program prints 3:45.
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.
Member.from_email should build a Member, so the program prints Member ana, but it prints User ana. Fix the class method.
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.
Each row shows name and count each time one of them changes, once both exist.
class Ticket:
issued = 0
def __init__(self, name):
self.name = name
Ticket.issued += 1
for name in ["ana", "ben", "cai"]:
ticket = Ticket(name)
count = Ticket.issuedSign 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: A @classmethod from_csv builds the product with cls; there is no make_product function.
This code works, but building a Product from a CSV line lives in a separate function. Replace make_product with a class method Product.from_csv(line) that uses cls, and call it instead. 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 Item(name, cents) with a class attribute catalog (a dictionary from name to item, filled in __init__), a static method parse_price(text) that turns "$3.50" into 350, a class method from_label(label) for labels like "tea: $2.75", and a class method cheapest() returning the item with the fewest cents. The program should print:
3 items cheapest: gum 95 tea 275
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.