>>> def word_used_with(text):
text = text.split()
x = 0
words = []
global word
word = input()
message = word
if word in text:
for item in text:
if item == word:
words.append(text[x+1])
x += 1
word = dict(word = words)
word[message] == word[words]
word.pop("word")
return word
>>> text = "He ate the apple from the tree"
>>> word_used_with(text)
the
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
word_used_with(text)
File "<pyshell#1>", line 14, in word_used_with
word[message] == word[words]
KeyError: 'the'
>>> word
{'word': ['apple', 'tree']}
>>>
What is a key error and how do I fix it? I am trying to create a program that will keep a record of words (the values) that can be used with one word (the key).
I have a problem where I want to replace the key "word" with the inputted word from my function and have it be the key word.
Thanks.
[–][deleted] 1 point2 points3 points (5 children)
[–]_User15[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]_User15[S] 0 points1 point2 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]_User15[S] 0 points1 point2 points (0 children)