-1

Okay I've been having some problems with my code, it's a simple python search engine whereby the user inputs a sentence and a word. If the word is in the sentence then the code is to output "word found" and if the word is not found then it is supposed to print "word not found".

Here is my code:

text = input()
word = input()

def wrong(text, word):
    print("Word not found")    
wrong(text, word)

def right(text, word):
    print("Word found")
right(text, word)

def check(right, wrong):
    if text.index == word:
        return right
    elif text.index != word:
        return wrong
check(right, wrong)

So instead what I've been getting as output is both the responses instead of just one. Please help me.

  • `if word in text: ... else: ...` – deceze Feb 05 '21 at 15:47
  • You're calling `wrong` and `right` right after you defined them, unconditionally… So yes, both will always print. – deceze Feb 05 '21 at 15:48
  • [`str.index`](https://docs.python.org/3/library/stdtypes.html#str.index) is a function that you must call, not something that you compare a string to. – deceze Feb 05 '21 at 15:50

2 Answers2

1

Are you searching for something like? :

a = 'some word'
b = 'some'

if b in a:
  print('yes')
else:
  print('no')
Andi Domi
  • 731
  • 2
  • 19
  • 48
0
text = input('input text: ')
word = input('input a word: ')

if word in text:
    print('Word found!')
else:
    print('Word not found')