جواب «Sum the Weights» چالش 1 از جلسهی 34
سؤال
تابع score را کامل کن: وزن هر کلمه را از فرهنگ بگیر (کلمهی ناشناس صفر) و جمع بزن. خروجی برای دو جمله باید 4 و -2 باشد.
راهنما
total = total + weights.get(word, 0).
یک جواب درست
weights = {"great": 2, "good": 1, "love": 2, "okay": 0.5,
"bad": -2, "boring": -1, "hate": -2, "slow": -1}
def score(sentence):
total = 0
for word in sentence.lower().split():
total = total + weights.get(word, 0)
return total
print(score("The game is great and I love it"))
print(score("boring and slow"))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن