بتل کد Battle Codeدارد باز می‌شود…

جواب «Step 3: Mark the Answer» چالش 3 از جلسه‌ی 16

سؤال

قدم ۳ از ۴: اگر بازی در جریان است (memory["index"] برابر -1 نیست)، پیام جواب سؤال فعلی است. اگر درست بود امتیاز بده و نتیجه Correct! است، وگرنه Wrong! The answer is <جواب درست>.. اگر سؤال دیگری مانده، [نتیجه، سؤال بعد] را برگردان (با ask)؛ وگرنه memory["index"] را -1 کن و [نتیجه، "Quiz over!"] را برگردان.

راهنما

i = memory["index"]. جواب درست questions[i]["answer"] است. «سؤال دیگری مانده» یعنی i + 1 < len(questions).

یک جواب درست

import bot

questions = [
    {"q": "What is 2 + 3?", "options": ["4", "5", "6"], "answer": "5"},
    {"q": "Which animal says moo?", "options": ["Cat", "Cow", "Duck"], "answer": "Cow"},
    {"q": "What colour is the sky on a sunny day?", "options": ["Blue", "Green", "Red"], "answer": "Blue"},
]
memory = {"index": -1, "score": 0}

def ask(i):
    bot.buttons(questions[i]["options"])
    return questions[i]["q"]


def reply(text):
    if text == "/quiz":
        memory["index"] = 0
        memory["score"] = 0
        return ask(0)
    i = memory["index"]
    if i == -1:
        return "Welcome to Quiz Bot! Send /quiz to play."
    if text == questions[i]["answer"]:
        memory["score"] += 1
        result = "Correct!"
    else:
        result = f"Wrong! The answer is {questions[i]['answer']}."
    if i + 1 < len(questions):
        memory["index"] = i + 1
        return [result, ask(i + 1)]
    memory["index"] = -1
    return [result, "Quiz over!"]


bot.run(reply)

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن