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

جواب «Step 4: The Final Score» چالش 4 از جلسه‌ی 16

سؤال

قدم ۴ از ۴: آخر بازی به‌جای Quiz over! بگو Quiz over! Your score: <امتیاز>/<تعداد سؤال‌ها> (تعداد را با len حساب کن) و یک دکمه‌ی /quiz برای بازی دوباره بگذار.

راهنما

قبل از return آخر: bot.buttons(["/quiz"]). پیام آخر با f و memory['score'] و 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
    bot.buttons(["/quiz"])
    return [result, f"Quiz over! Your score: {memory['score']}/{len(questions)}"]


bot.run(reply)

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