I need help in my Python

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

can you give more details on what this code should do? I have no idea just looking at it, so I have no idea how to help you

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

can you give input examples of it not working how you expect?

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")


These were the errors I got

It’s meant to search the variable; text in the variable; word
And both, will be an input from the user
It’s a test

If you look at it, in my initial code,I tried to print what words (after the split) should look like and I am getting only the

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.

1 Like
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.

1 Like