forked from jmelahman/python-for-everybody-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise9_1.py
executable file
·28 lines (24 loc) · 869 Bytes
/
exercise9_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
"""
Exercise 9.1: Write a program that reads the words in words.txt and stores
them as keys in a dictionary. Download a copy of the file from
https://www.py4e.com/code3/words.txt. It doesn't matter what the values are.
Then use the 'in' operator as a fast way to check whether a string is in the
dictionary.
Python for Everybody: Exploring Data Using Python 3
by Charles R. Severance
"""
count = 0
dictionary_words = dict() # Initializes the dictionary
fhand = open('words.txt')
for line in fhand:
words = line.split()
for word in words:
count += 1
if word in dictionary_words:
continue # Discards duplicates
dictionary_words[word] = count # Value is first time word appears
if 'Python' in dictionary_words:
print('True')
else:
print('False')