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

جواب «Step 2: Start the Quiz» چالش 2 از جلسه‌ی 16

سؤال

قدم ۲ از ۴: صفحه‌ی play و دکمه‌ی Play اضافه شده‌اند. دو تابع را بنویس. show_question() در متن progress بنویسد Question 1 of 2 (شماره از ۱ شروع می‌شود)، در متن question متن سؤال شماره‌ی index را بگذارد و کادر Your answer را خالی کند. start_quiz(): اگر سؤالی نبود پیام Make some questions first! بدهد و در همان صفحه بماند؛ وگرنه index و score را صفر کند (با global)، show_question() را صدا بزند و صفحه‌ی play را باز کند.

راهنما

progress.value = f"Question {index + 1} of {len(questions)}" و question.value = questions[index]["q"]. در start_quiz خط اول global index, score است؛ بعد if len(questions) == 0: با پیام و return.

یک جواب درست

import app

questions = app.load("questions", [])
index = 0
score = 0

def show_questions():
    qlist.clear()
    for item in questions:
        qlist.add(f"{item['q']} = {item['a']}")

def add_question():
    q = q_box.value.strip()
    a = a_box.value.strip()
    if q == "" or a == "":
        app.toast("Write a question and its answer")
        return
    questions.append({"q": q, "a": a})
    app.save("questions", questions)
    q_box.value = ""
    a_box.value = ""
    show_questions()

def show_question():
    progress.value = f"Question {index + 1} of {len(questions)}"
    question.value = questions[index]["q"]
    answer_box.value = ""

def start_quiz():
    global index, score
    if len(questions) == 0:
        app.toast("Make some questions first!")
        return
    index = 0
    score = 0
    show_question()
    app.go("play")

def check_answer():
    pass

app.screen("make")
app.title("Quiz Maker")
q_box = app.input("Question")
a_box = app.input("Answer")
app.button("Add question", add_question)
app.button("Play", start_quiz)
qlist = app.list()

app.screen("play")
app.title("Quiz")
progress = app.text("")
question = app.text("")
answer_box = app.input("Your answer")
app.button("Check", check_answer)

show_questions()
app.run()

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