你在寫日記,想看看哪些詞用得多。寫 word_tally(sentence),返回字典:每個詞出現幾次。不分大小寫(都變成小寫),去掉詞兩頭的 .,!?,按第一次出現的順序。空句子返回 {}。
word_tally("The sun is up. The sky is blue!")→{'the': 2, 'sun': 1, 'is': 2, 'up': 1, 'sky': 1, 'blue': 1}word_tally("Go, go, GO!")→{'go': 3}def word_tally(sentence):
tally = {}
for word in sentence.split():
# 在這裡寫:變小寫、去掉兩頭的 .,!?
# 再數進 tally
pass
return tally
寫好了先點「執行」試試示例,沒問題再點「提交」。