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

جواب «Five Is Enough» چالش 8 از جلسه‌ی 6

سؤال

یک لیست کوتاه بهتر انجام می‌شود! اگر از قبل ۵ کار در لیست هست، کار تازه اضافه نشود و پیام Your list is full بیاید. شمارنده Tasks: 5 بماند.

راهنما

قبل از append: if len(todos) >= 5: و پیام و return. فکر کن: > 5 یا >= 5؟ با ۵ کار، ششمی نباید برود.

یک جواب درست

import app

todos = []

def show_list():
    items.clear()
    for t in todos:
        items.add(t)
    count.value = f"Tasks: {len(todos)}"

def add():
    text = box.value.strip()
    if text == "":
        app.toast("Write a task first")
        return
    if len(todos) >= 5:
        app.toast("Your list is full")
        return
    todos.append(text)
    box.value = ""
    show_list()

app.title("My Tasks")
box = app.input("New task")
app.button("Add", add)
items = app.list()
count = app.text("Tasks: 0")
show_list()
app.run()

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