جواب «Step 2: Add» چالش 2 از جلسهی 9
سؤال
قدم ۲ از ۴: کادر New task و دکمهی Add آمادهاند. add() را بنویس: متن را strip کن؛ متن خالی را با پیام Write a task first رد کن؛ کاری که متنش از قبل در لیست هست را با پیام Already in the list رد کن؛ وگرنه {"text": ..., "done": False} اضافه کن، کادر را خالی کن و لیست را از نو بکش.
راهنما
همان add جلسهی ۶ و ۷. برای تکراری: for t in todos: و if t["text"] == text: با app.toast("Already in the list") و return.
یک جواب درست
import app
todos = [
{"text": "Brush my teeth", "done": True},
{"text": "Pack my school bag", "done": False},
]
def show_list():
items.clear()
for t in todos:
items.add(t["text"], done=t["done"])
def add():
text = box.value.strip()
if text == "":
app.toast("Write a task first")
return
for t in todos:
if t["text"] == text:
app.toast("Already in the list")
return
todos.append({"text": text, "done": False})
box.value = ""
show_list()
app.title("My Day")
box = app.input("New task")
app.button("Add", add)
items = app.list()
show_list()
app.run()
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن