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

جواب «Score a Sentence» چالش 3 از جلسه‌ی 33

سؤال

تابع score(sentence) را کامل کن: برای هر کلمه WEIGHTS.get(word, 0) را به total اضافه کن. خروجی:
4 great fun
-3 so boring and slow
-1 i hate this but i love the music

راهنما

for word in sentence.split(): total += WEIGHTS.get(word, 0)، قبل از return.

یک جواب درست

WEIGHTS = {"great": 2, "fun": 2, "love": 2, "nice": 1, "cool": 1, "kind": 2,
           "boring": -2, "bad": -1, "stupid": -3, "ugly": -2, "hate": -3, "slow": -1}

def score(sentence):
    total = 0
    for word in sentence.split():
        total += WEIGHTS.get(word, 0)
    return total

for s in ["great fun", "so boring and slow", "i hate this but i love the music"]:
    print(score(s), s)

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