- 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))
5 Réponses
+ 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))
+ 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 ❤️❤️❤️❤️❤️
+ 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")
+ 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))
+ 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))