Question: > 
> Write a program that reads a sentence. The program then prints a table of word frequencies. The words are in alphabetical order with the number of times each word occurs. The words are case insensitive. The program has a word\_frequencies(words) function that takes string input. It builds a word frequency dictionary from the input words string and returns the dictionary of word frequencies.
> 
> 
> 

```
def word_frequencies(words):
# Implement the word_frequencies function.  The words function parameter is a string.
# Your solution here
    freq = {}
    for word in words:
        freq[word] = words.count(word)
    return freq

# The main program.  Do not change it.
if __name__ == '__main__':
    words = input("Enter a sentence: ")
    your_dictionary = word_frequencies(words)
    sorted_keys = sorted(your_dictionary.keys())
    for key in sorted_keys:
        print(key + ': ' + str(your_dictionary[key]))

```

This is the code I have but it's returning the frequencies for letters and spaces, not words when entering in ZyBooks now zyBooks later zyBooks forever

```
 : 5
B: 3
Z: 1
a: 1
e: 3
f: 1
k: 3
l: 1
n: 1
o: 8
r: 3
s: 3
t: 1
v: 1
w: 1
y: 3
z: 2

```

Answer: 
when using a for loop and the .count() method, we should iterate over a `set` of the words list. this avoids duplicated and unnecessary calls of the .count() method.

```
def word_frequencies(words):
    freq = {}
    words = words.split()
    for word in set(words):
        freq[word] = words.count(word)
    return freq

# The main program.  Do not change it.
if __name__ == '__main__':
    words = input("Enter a sentence: ")
    your_dictionary = word_frequencies(words)
    sorted_keys = sorted(your_dictionary.keys())
    for key in sorted_keys:
        print(key + ': ' + str(your_dictionary[key]))

```
You just need to `split()` the input into list of words. And you can also use dict comprehension:

```
def word_frequencies(words):
# Implement the word_frequencies function.  The words function parameter is a string.
# Your solution here
     return {word: words.count(word) for word in words.split()}

```