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!
def measure(results, goal):
right = 0
print("right:", right, "/", len(results))
# ---- 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)
راهنما و جواب این چالش · برگشت به درس جلسهی 47: پروژهی نهایی ۲: ساختن و آزمودن
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال