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 Circle(radius) where radius is a property whose setter raises ValueError("radius must be positive") unless the value is above 0, and area is a read-only property giving round(3.14159 * radius ** 2, 2). Print the area of Circle(2), change its radius to 3 and print the area again, then try setting the radius to -1 and print the error message.
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 turns the second price method into the setter, so the program prints 5 and then refused.
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 Ada and then GRACE, but it fails with RecursionError. Fix the getter and setter so they use an internal attribute.
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 side and area each time one of them changes, once both exist.
class Square:
def __init__(self, side):
self.side = side
@property
def area(self):
return self.side * self.side
sq = Square(2)
area = sq.area
for side in [3, 4]:
sq.side = side
area = sq.areaSign 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: volume is a property with a setter; there are no get_volume or set_volume methods.
This code works, but it uses Java-style get_volume and set_volume methods. Replace them with a volume property and setter (keeping the 0 to 10 limit), and use speaker.volume in the calls. 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 Member(username, email). username is a property whose setter strips spaces, lower-cases it and raises ValueError("username must be 3 to 12 letters or digits") unless the result is 3 to 12 characters and isalnum(). email is read-only after creation and must contain @ (else ValueError("invalid email")). handle is a read-only property giving @ plus the username. The program should print:
@mia_dev? no: username must be 3 to 12 letters or digits @miadev mia@example.com @mia2 email is read-only invalid email
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.