text = input()
word = input()
print (word)
words = word.split()
print (text)
print (words)
if text in words:
print("Word found")
else:
print("Word not found")
I can’t find what I am doing wrong on this code
It doesn’t print Word found, so I debugged using the print statement to see what the variable “words” print, at first, it works fine, then it doesn’t. Please assist me
Well, it’s to take an input from the user for both text and word, and then, check if the text is one of the strings contained in the word variable written
The problem is you’re confused which text is to be search upon which string of text.
text = "hello welcome to the" //string
word = 'the' //substring to be searched
words = text.split()
if word in words:
print("Word found")
else:
print("Word not found")
You’re searching the input text which is the original strings of text in your IF statement again. Change the ‘text’ variable in your IF statement to ‘word’ because thats the one you’re going to search.
text = input('enter: ')
word = input('eg_word: ')
print(f'input is : {text}\neg is: {word}')
if word in text:
print('word found')
else:
print('word not found')
you don’t need to split it. Because when you enter the sentence you the text was already split by blank space.str can direct traversal.