Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

given word as input not find out in text but still it return Word found as output instead of Word not found. So can you correct it?

# enter code here
text=input()
word=input()
def search(text ,word):
    if (text.find(word)):
        print('Word found')
    else:
        print('Word not found')

search(text, word)

>Solution :

find method returns the index from where a word is found otherwise -1.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

which means if the word is not found, text.find(word) will return -1 and -1 is truthy value. Thus the condition for if statement becomes truthy always, thus always printing word found.

So, one way to get it right is to do this;

# enter code here
text=input()
word=input()
def search(text ,word):
    if (text.find(word) != -1): # only change here
        print('Word found')
    else:
        print('Word not found')

search(text, word)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading