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

جواب «A Bag of Words» چالش 2 از جلسه‌ی 33

سؤال

یک جمله با input خوانده می‌شود. هر کلمه‌اش (sentence.split()) را در دیکشنری counts بشمار و چاپ کن. برای the game is fun and the music is fun:
{'the': 2, 'game': 1, 'is': 2, 'fun': 2, 'and': 1, 'music': 1}

راهنما

for word in sentence.split(): counts[word] = counts.get(word, 0) + 1.

یک جواب درست

sentence = input().lower()
counts = {}
for word in sentence.split():
    counts[word] = counts.get(word, 0) + 1
print(counts)

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