- 1

Python task to define a function that checks if a word existed in a text.. still got this code wrong

text = input() word = input() def sch ( t , w ) : for i in text : if i == word : print("Word found") else : print("word not found") return print(sch(text, word))

8th Dec 2021, 5:05 PM
Amr Monsef
Amr Monsef - avatar
5 Answers
+ 2
Not optimal do this: def sch ( t , w ) : if w in t: a= 'ye' else: a= 'no' return a text = input() word = input() print(sch(text, word))
8th Dec 2021, 5:22 PM
Amirreza
Amirreza - avatar
+ 2
text = input() word = input() a='' def sch(t,w): if word in text: a = "Word found" elif word not in text : a = "Word not found" return a print(sch(text, word)) THANK YOU ALL EVERY SINGLE COMMENT WAS JUST TOO HELPFUL ❤️❤️❤️❤️❤️
8th Dec 2021, 5:29 PM
Amr Monsef
Amr Monsef - avatar
+ 1
Hello Amr Monsef if your funtion already print if a word is found or not, then just call this function: sch(text, word) Your function does not need a return statement. And I think you don't need a loop. if word in text: print("Word found") else: print("Word not found")
8th Dec 2021, 5:15 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
word = input() sentence = input() def sub_string(w, s): if w in s: return "Word found" else: return "Word not found" print(sub_string(word, sentence))
8th Dec 2021, 5:28 PM
Mafdi
Mafdi - avatar
+ 1
Amr Monsef You're welcome. Also if u like to solve it with a loop do this: t=input().split() w=input() def sch(t,w): for i in t: if i== w: a= "Word found" break else : a= "word not found" return a print(sch(t, w))
8th Dec 2021, 5:41 PM
Amirreza
Amirreza - avatar