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. Python looks for each method on the object's own class first.
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.
Given the Pet class in the starter, write a subclass Cat that overrides sound() to return Meow. Print Cat("Tom").introduce().
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 a Student gets its name from Person.__init__ and the program prints Ana in year 2.
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.
Premium members should pay half price, so the program should print 20.0 and 10.0. The premium price is not applied. Fix the subclass.
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 cost and total each time one of them changes, once both exist.
class Item:
def __init__(self, price):
self.price = price
def cost(self):
return self.price
class Taxed(Item):
def cost(self):
return self.price * 2
total = 0
for item in [Item(3), Taxed(4), Item(1)]:
cost = item.cost()
total = total + costSign 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 Vehicle with __init__ and label once; Car and Truck inherit from it and define only max_load.
This code works, but Car and Truck repeat the same __init__ and label. Move them into a parent class Vehicle that both inherit from, keeping only max_load in each child. 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 Employee(name, monthly) with pay() returning the monthly amount and __str__ returning name: pay. Write Manager(Employee), which adds a bonus in __init__ (calling super().__init__) and whose pay() adds the bonus to the parent's pay. Write Intern(Employee), whose pay() is half the parent's pay. Print each person and the total payroll. The output should be:
Ana: 3000 Ben: 4500 Cy: 1000.0 Total: 8500.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.