جواب «The App Skeleton» چالش 6 از جلسهی 46
سؤال
اسکلت هر سه مسیر یک «مسیریاب» پیام است. handle(message):
- /help → HELP.
- اگر با /ask شروع شد: باقی متن (message[5:]) را با ask بپرس و جواب را برگردان؛ اگر باقی متن خالی بود (/ask تنها) → Type a question after /ask.
- /quiz → سؤال بعدی QUIZ به نوبت (بعد از آخری دوباره از اول)، با یک شمارندهی global quiz_index.
- هر چیز دیگر → I don't understand. Type /help.
خروجی test با HELP شروع میشود، بعد (model) why is the sky blue، بعد سه سؤال کوییز (Q1، Q2، دوباره Q1).
راهنما
message.startswith("/ask ") و question = message[5:].strip(). کوییز: q = QUIZ[quiz_index]، quiz_index = (quiz_index + 1) % len(QUIZ). /ask تنها با /ask شروع نمیشود (فاصله ندارد)؛ برایش یک if message == "/ask" بنویس.
یک جواب درست
from ai import ask
HELP = "Commands: /help, /ask <question>, /quiz"
QUIZ = ["Q1: How many moons does Nilu have?", "Q2: When does Sprout water the beds?"]
quiz_index = 0
def handle(message):
global quiz_index
if message == "/help":
return HELP
if message == "/ask" or message.startswith("/ask "):
question = message[5:].strip()
if question == "":
return "Type a question after /ask"
return ask(question)
if message == "/quiz":
q = QUIZ[quiz_index]
quiz_index = (quiz_index + 1) % len(QUIZ)
return q
return "I don't understand. Type /help"
# ---- test with a pretend model (do not change) ----
def fake_ask(prompt, system=None, history=None):
return "(model) " + prompt
ask = fake_ask
for message in ["/help", "/ask why is the sky blue", "/quiz", "/quiz", "/quiz", "/ask", "hello"]:
print(handle(message))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن