A simple question, that I've actually had a hard time finding a solution to, as most suggest using the much easier to work with, for loop.
I have an assignment that tells me explicitly to use the while loop to return either true or false, based on if a string occurs in a list.
My current solution (which works) is as follows:
def word_in_list(word, words):
x=0
length = int(len(words)) -1
while words[x] != word and x < length:
x = x+1
if words[x] == word:
print (True)
elif x == length and words[x] != word:
print (False)
My biggest issue has been limiting the function to the length of the list, otherwise I get an error stating "list index out of range".
However, as this is a rather clunky way of doing something so simple, I'm looking for suggestions on how to streamline it a little - if there's a way that is. And as stated, the for-loop, that you'd normally use, is not an option.