The Not Rule چالش 5 از جلسهی 33، سخت
قانون «نه» را اضافه کن: اگر کلمه not بود، flip = True و برو کلمهی بعد (continue). وگرنه w = WEIGHTS.get(word, 0)؛ اگر flip بود و w صفر نبود، w = -w و flip = False. بعد total += w. خروجی:1 this is not bad-2 not great at all5 i do not hate it it is fun-2 boring
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
flip = False
for word in sentence.split():
total += WEIGHTS.get(word, 0)
return total
for s in ["this is not bad", "not great at all", "i do not hate it it is fun", "boring"]:
print(score(s), s)
راهنما و جواب این چالش · برگشت به درس جلسهی 33: خوب یا بد؟ · چالش بعدی: Learn the Weights
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال