-2

This is my assignment:

# text = input()
# word = input()
# print(search(text, word))

I need to print "Word found" if word there is in text. And "Word not found" if doesn't.

How I tried:

  1. First attempt:

    text = input()
    word = input()
    def search(text, word):
        if text.split() in word.split():
            print("Word found")
        else:
            print("Word not found")
    search(text, word)
    
  2. Second attempt:

    def search(text, word):
        if word.find() == text.find():
            print ("!")
        else:
            print("?")
    search(text, word)
    
  3. Third attempt:

    text = input()
    word = input()
    def answer(text, word):
        if text.find(word):
            True
            print("!")
        else:
            print("?")
    answer(text, word)
    
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Lili Soto
  • 17
  • 1

3 Answers3

3

You can simply use in:

def search(text, word):
    if word in text:
        print("Word found")
    else:
        print("Word not found")

text = input()
word = input()

search(text, word)
Marco_CH
  • 3,243
  • 8
  • 25
3
text = input()
word = input()
def search(t, w):
  return "Word found" if t in w else return "Word not found"

print(search(text, word))

i think this could be a solution for you, if you think it's best you can have a normal if else instead of the ternary if as well.

1

Try using this:

S = "find the word"
if "word" in S