جواب «Handle "not"» چالش 3 از جلسهی 34
سؤال
قاعدهی «نه» را اضافه کن: اگر کلمهی قبلی not بود، وزن کلمهی فعلی را برعکس کن. متغیر previous را درست بهروز کن. خروجی برای سه جمله: -1، 2 و 0.
راهنما
if previous == "not": w = -w و در انتهای حلقه previous = word.
یک جواب درست
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)
if previous == "not":
w = -w
total = total + w
previous = word
return total
print(score("not good"))
print(score("not bad at all"))
print(score("good but not great and not slow"))
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن