我有一個小的家庭作業問題,讓我的Python函數返回正確的值。Python函數的字典
分配:
Here is an example dictionary:
word_freq = {'the': 58, 'people': 6, 'beautiful': 8, 'cats': 13}
Write a function that takes as parameters: a word frequency dictionary and a threshold number with is an integer. This function should return a result that is a list of all the words whose frequency is greater than the threshold.
For example, suppose that you name your function top_words. If you call the function with the dictionary above and the threshold of 10:
top_words(word_freq, 10) you should get the result [‘the’, ‘cats’]
我的代碼:
def frequencylist(frequentwords, threshold):
for keys in frequentwords:
if key > threshold:
print(keys)
試運行:
frequentwords= {'hello':30, 'yellow':4, 'red':10, 'blue':35, 'orange':100}
frequencylist(frequentwords, 10)
...我得到這個錯誤:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in frequencylist
TypeError: unorderable types: str() > int()
未來,請採取簡單的調試步驟:打印出來的在錯誤點之前發現值。再次運行;最後的輸出可能會清楚地顯示你的問題。 – Prune