Handle "not" چالش 3 از جلسهی 34، متوسط
قاعدهی «نه» را اضافه کن: اگر کلمهی قبلی not بود، وزن کلمهی فعلی را برعکس کن. متغیر previous را درست بهروز کن. خروجی برای سه جمله: -1، 2 و 0.
weights = {"great": 2, "good": 1, "love": 2, "okay": 0.5,
"bad": -2, "boring": -1, "hate": -2, "slow": -1}
def score(sentence):
total = 0
previous = ""
for word in sentence.lower().split():
w = weights.get(word, 0)
# flip w if previous == "not"
total = total + w
# remember this word
return total
print(score("not good"))
print(score("not bad at all"))
print(score("good but not great and not slow"))
راهنما و جواب این چالش · برگشت به درس جلسهی 34: احساس مثبت یا منفی · چالش بعدی: Learn the Weights
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال