topskill.dev
CoursesTracks
0
All courses/Object Oriented Programming with PythonRun a scripted shopping session
Project — Console shop
13 / 15
← Object Oriented Programming with Python
○Define a class○Create two objects○Attach instance data○Read and update data○Class defaults○Shadow a class default○Write an instance method○Return a value○Initialize an object○Constructor defaults○Independent instance lists○Class body scope✦Module Quiz
○Bound methods○Function attributes○Classes are callable○A class method factory○Static methods○The property function○The property decorator○Computed properties○Read-only properties○Property setters✦Module Quiz
○Give objects a friendly string○Make repr useful for debugging○Add two value objects○Subtract value objects○Multiply a value by a scalar○Support reflected multiplication○Compare objects by value○Order value objects○Give an object a length○Choose an object truth value○Build a safe hashable key○Make an instance callable○Customize formatted output○Keep cleanup explicit✦Module Quiz
○Inherit an existing method○Recognize the object base○Extend initialization with super○Override one behavior○Extend a method through super○Substitute objects in a loop○Compose a service from a collaborator○Reserve attributes with slots○Keep slots in a subclass○Combine small inherited capabilities○Inspect method resolution order○Cooperate through a diamond✦Module Quiz
○Start an animal record○Record a rescued bird○Record a rescued fish○Give each animal a movement○Add a feeding service○Keep a rescue register○Group animals by habitat○Print the daily care report✦Module Quiz
○Open an account in cents○Deposit money○Withdraw available money○Add savings account data○Allow a current-account overdraft○Lock a fixed deposit○Calculate simple interest separately○Credit calculated interest○Register accounts by ID○Print a current statement✦Module Quiz
○Name a fixed set of statuses○Choose explicit enum values○Generate internal values with auto○Compare members, not raw codes○Reject duplicate enum values○Iterate over canonical members○Give enum members a method○Choose an enum truth rule○Check the enum type at a boundary○Describe a small dataclass record○Store an enum member in a dataclass○Use dataclass objects as enum values✦Module Quiz
○Intercept an attribute read○Handle class-level descriptor access○Intercept an attribute assignment○Keep descriptor storage per instance○Delete a managed value○Let the class supply a field name○Give a data descriptor priority○Allow a non-data descriptor override○Recognize property as a descriptor○Connect function binding to descriptors○Reuse one validation rule across fields○Initialize a descriptor-backed record✦Module Quiz
○Reject an invalid lobby size○Catch only the expected lookup error○Name a domain error○Carry useful exception details○Preserve the cause of a failure○Release a resource with finally○Declare an abstract contract○Implement a credit fee policy✦Module Quiz
○Validate a product record○Build a searchable catalog○Calculate a cart line○Add products to a cart○Remove a cart line○Summarize the cart○Freeze an order snapshot○Checkout without losing the cart○Register and track an order○Format invoice lines○Render the complete invoice○Route shop commands●Run a scripted shopping session○Launch the console shop✦Module Quiz

Run a scripted shopping session

Process a list of console commands while recovering from expected user errors.

A scripted session makes the command-line workflow repeatable without blocking on input(). The caller supplies a Shop and an iterable of command strings. Return one response per attempted command. Catch ValueError and KeyError at this user-facing boundary, prefixing their text with Error: . Other failures should still be visible to a developer. A stripped command equal to quit ends the session without adding a response.

A bad command does not discard earlier responses. Append its error message at the same position and move on to the next command. By contrast, quit is a control command: it adds no response and stops consuming the remaining iterable. This separation makes a transcript easy to inspect.

A smaller example

The word loop stops before recording the stop marker or anything after it.

python
responses = []
for word in ["north", "stop", "south"]:
    if word == "stop":
        break
    responses.append(word.upper())
print(responses)

Your Task

  1. Implement run_session(shop, commands) to collect shop.execute responses in order until stripped quit. Convert only ValueError and KeyError into "Error: " + str(error) and continue; propagate other errors. Empty input returns an empty list.

Run and check

Edit main.py, then press Run. The supplied probe catches unfinished-code errors so you can inspect the Console before submitting. The finished responses show an addition, creation of O1, and its placed status. Press Submit to check the behavior with other inputs.

Hint 1
1 / 1
HINT 1

Put try/except inside the loop so one bad command does not end the session.

Files
  • ⋄main.py
Loading editor…
Press Run to execute your code.