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 Score(player, points) with a __repr__ returning Score('ana', 12)-style text, an __eq__ comparing points, and a __lt__ comparing points. Then print sorted(scores), max(scores) and whether Score('x', 9) == Score('y', 9).
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 special method name so len(basket) works and the program prints 3 items and then True.
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.
Adding two Distance objects should give a new Distance, so the program prints Distance(7 km) and 3. Right now the sum prints as the plain number 7. Fix __add__.
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 i and total each time one of them changes, once both exist.
class Week:
def __init__(self, hours):
self.hours = hours
def __len__(self):
return len(self.hours)
def __getitem__(self, index):
return self.hours[index]
week = Week([8, 6, 7])
total = 0
for i in range(len(week)):
total = total + week[i]Sign 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: Use __str__ with print(red) and __eq__ with ==; no describe or same_as.
This code works, but it uses home-made method names. Replace describe with __str__ and same_as with __eq__, and change the calls to print(c) and ==. 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 Vector(x, y) with: __repr__ giving Vector(1, 2); __add__ and __sub__ with another vector; __mul__ and __rmul__ with a number (so v * 2 and 2 * v both work); __abs__ giving the length (x*x + y*y) ** 0.5; __eq__; and __bool__ that is False only for the zero vector. The program should print:
Vector(4, 6) Vector(-2, -2) Vector(2, 4) Vector(2, 4) 5.0 True False False
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.