I have a function which takes 2 arguments - an input string, and a list of words. For every word in the list which is present in the string, the function returns "True", else "False".
My code is given below:
#!/usr/bin/python
# inputStr is a string, inputList is list of strings
def keyword_usage(inputStr, inputList):
splitStr = inputStr.split();
L = [];
k = 0;
bool = 0;
for i in range(0,len(inputList)):
for j in range(0, len(inputStr)):
if inputList[i]==splitStr[j]:
bool = 1;
else:
bool = 0;
if bool==1:
L[k] = "True";
else:
L[k] = "False";
k+=1;
return tuple(L);
I am running it in the interpreter as shown below:
>>> from keyword_usage import keyword_usage
>>> res = keyword_usage('Dive Into Python', ['Python', 'python', 'scala'])
When I press enter, I get this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "keyword_usage.py", line 17, in keyword_usage
if inputList[i]==splitStr[j]:
IndexError: list index out of range
I am new to Python coding and am very confused. It seems a basic question, but I could not get any answer on any forum. Can someone spot the error? Thanks in advance!!