Exercise 11.1. Write a function that reads the words in words.txt and stores them as keys in a
dictionary. It doesn’t matter what the values are. Then you can use the in operator as a fast way to
check whether a string is in the dictionary.
Trying to figure this one out, but no luck so far. I am trying to get the function to open words.txt, count the length of the word, use word as key and the len(word) as the value for input into the dictionary. I'm pretty sure this is not the right way to add these items into the dictionary but I can't find it anywhere only how to manually add them.
If I type engdicdefine()
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
engdicdefine()
File "C:\Users\Jeremy\Google Drive\Trident Module saves\Duncan - Module 3 Case\Module 3 Think Python Chap11.py", line 65, in engdicdefine
engwords=dict(wordcount)
ValueError: dictionary update sequence element #0 has length 4; 2 is required
If I type engwords, I get:
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
engwords
NameError: name 'engwords' is not defined
def engdicdefine():
fin = open('words.txt')
line = fin.readline()
word = line.strip()
count=0
for word in fin:
count=len(word)
wordcount=(word, count)
engwords=dict(wordcount)