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. Watch where each assignment stores its value.
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 Song whose __init__ stores title and seconds, with a __str__ that returns text like Halo (3:41), the seconds shown as minutes and two-digit seconds. Print Song("Halo", 221) and Song("Intro", 65).
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 coffee is medium unless another size is given.
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.
Task 05 .Fill the table with the variable values, step by step.
Each row shows name and last each time one of them changes, once both exist. last copies the newest member's number.
class Member:
next_id = 1
def __init__(self, name):
self.name = name
self.id = Member.next_id
Member.next_id += 1
last = 0
for name in ["ana", "ben", "cy"]:
member = Member(name)
last = member.idSign 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: Product.__init__ raises ValueError for a negative price; the caller no longer checks price itself.
This code works, but every caller must remember to check the price before creating a Product. Move the check into __init__, raising ValueError for a negative price, and let the caller just create products inside try. 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 Card with a class attribute fee = 30. __init__(self, name, visits=None) stores the name title-cased and a fresh list of visit days (a copy of visits if given). Add visit(day), __str__ returning Name: N visits and __repr__ returning Card('Name', N). Raise ValueError if the name is blank. The starter's steps should print:
Ana: 2 visits
Ben: 0 visits
[Card('Ana', 2), Card('Ben', 0)]
blank nameSign 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.