جواب «Did You Reach Your Goal?» چالش 6 از جلسهی 47
سؤال
results لیستی از (expected, reply) است. measure(results, goal):
- right = تعداد جوابهایی که expected داخلشان است؛ percent = right * 100 // len(results).
- چاپ کن right: 7 / 10 = 70 %.
- اگر percent >= goal: goal met!؛ وگرنه حساب کن چند جواب درست دیگر لازم است تا به goal برسی (با یک حلقهی while که need را یکی یکی زیاد کند تا (right + need) * 100 // len(results) >= goal) و چاپ کن goal not met: 1 more right answer needed.
خروجی test:right: 7 / 10 = 70 %goal not met: 1 more right answer needed---right: 5 / 6 = 83 %goal met!
راهنما
need = 0 و while (right + need) * 100 // len(results) < goal: need += 1.
یک جواب درست
def measure(results, goal):
right = 0
for expected, reply in results:
if expected in reply:
right += 1
percent = right * 100 // len(results)
print("right:", right, "/", len(results), "=", percent, "%")
if percent >= goal:
print("goal met!")
else:
need = 0
while (right + need) * 100 // len(results) < goal:
need += 1
print("goal not met:", need, "more right answer needed")
# ---- test (do not change) ----
results = [
("Tuesday", "The club meets every Tuesday."),
("12", "There are 12 members."),
("4", "There are 4 tomato beds."),
("3 liters", "Each bed needs water."),
("84", "84 (calculator)"),
("Sprout", "Sprout waters the beds."),
("solar", "It runs on a battery."),
("Fridays", "On Fridays."),
("I don't know", "I don't know. The handout does not say."),
("I don't know", "Paris is the capital of France."),
]
measure(results, 80)
print("---")
measure(results[:6], 80)
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن