In: Computer Science
Create a program that reports whether each word in a set of words appears in a paragraph. Here are the requirements: a. Ask the user to enter a set of words, one at a time. Use an appropriate prompt to keep asking for words until a blank line is entered. The set of words should be in a dictionary, where the word is the key, and “yes” or “no” is the value. The value represents whether the word appears in the paragraph. As the words are entered, the value should initially be “no”. b. Ask the user to enter a paragraph. Use an appropriate prompt to keep asking for lines/sentences of the paragraph. The lines/sentences should be in a list (of strings). When the user enters a blank line, the paragraph is complete. c. Once the paragraph is entered, check to see if each word in the dictionary is in the paragraph. If it is, then set the value of that key (word) to “yes”. It is sufficient to determine if the word is in the paragraph. You do not need to find all instances, nor count the number of instances. d. Present the results by writing the results of the dictionary using a loop. (Do not simply print the dictionary as a whole.) This should appear as two columns of data, with the key in the first column, and the corresponding value of “yes” or “no” in the second column.
Code:
import requests
url = "http://jsonplaceholder.typicode.com/todos"
class ApiHandler:
def __init__(self):
self.url = url
def get_todos(self):
try:
result = requests.get(self.url)
if result.status_code == 200:
return result.response
else:
return None
except TimeoutError as te:
print('here')
def mul(self,a,b):
return a*b
words = {} # initialize the words dict
print("Continue entering words and stop with input as blank word")
while True:
word = input() # input single word
if word: # if input is present that is not a blank line then add to dict
words[word] = 'no' #initialize
else:
break
lines = []
print("Continue entering lines and stop with input as blank line")
while True:
line = input()
if line:
lines.append(line) # adding input in list lines
else:
break
for word in words: # for each word in the dict words
for line in lines: #for each lines in the list lines
if word in line: # if line has the word then then store dict[key] = "yes" then break as we don't want countor anything as such in the paragraph
words[word] = 'yes'
break
for word in words:
print(word + "\t" + words[word]) # print the word and the value i.e yes/no
Screenshot of the code:
Output: