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

جواب «Check the Plan» چالش 3 از جلسه‌ی 46

سؤال

check_plan(plan) لیست مشکل‌ها را برگرداند: برای هر کلید REQUIRED که نیست یا خالی است ("" یا [])، missing + کلید؛ و اگر goal هست ولی هیچ رقمی ندارد، the goal needs a number. خروجی test:
plan OK
['missing sessions', 'the goal needs a number']
['missing path', 'missing user', 'missing does', 'missing sessions', 'missing goal']

راهنما

if key not in plan or plan[key] == "" or plan[key] == []:. برای رقم: حلقه روی str(plan["goal"]) و ch.isdigit().

یک جواب درست

REQUIRED = ["path", "user", "does", "sessions", "goal"]

def check_plan(plan):
    problems = []
    for key in REQUIRED:
        if key not in plan or plan[key] == "" or plan[key] == []:
            problems.append("missing " + key)
    if "goal" in plan:
        has_number = False
        for ch in str(plan["goal"]):
            if ch.isdigit():
                has_number = True
        if not has_number:
            problems.append("the goal needs a number")
    return problems


# ---- test (do not change) ----
good = {"path": "B", "user": "kids in my class", "does": "answers questions from our science handout",
        "sessions": [42, 44, 45], "goal": "8 of 10 test questions right"}
vague = {"path": "A", "user": "everyone", "does": "a fun game", "sessions": [], "goal": "it should be cool"}
empty = {}
for plan in [good, vague, empty]:
    problems = check_plan(plan)
    if len(problems) == 0:
        print("plan OK")
    else:
        print(problems)

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