Command Palette

Search for a command to run...

Hectal
Phase 8: Object-Oriented Python
Python · Exercise 6 of 6 Previous
Advanced

A swappable client with a dataclass result

Write a dataclass Reply with fields text: str and ok: bool = True.

Write a class FakeClient with __init__(self, answer) storing it, a list calls (a new empty list for each FakeClient), and a method complete(prompt) that records the prompt in calls and returns Reply(self.answer).

Write a function ask(client, question) that calls client.complete("Q: " + question) and returns the reply's text.

What Check tests

  1. Reply('hi') has ok set to True and compares by value
  2. ask returns the reply text
  3. The prompt is sent as 'Q: ' plus the question
  4. Each FakeClient has its own call list
  5. Any object with complete works

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.