+ 1
How to make the final project of python for beginners"search engine" with function?
I made the project using "if~else" statement and the code is :- text = input() word = input() if word in text: print ("Word found") else: print ("Word not found") # I want you guys to make this code again using "function".
4 Answers
+ 3
Function need to be reusable so consider adding parameter to function, like this:
text = input()
word = input()
def search(textForSearch,wordForSearch):
if wordForSearch in textForSearch:
return "Word found"
else:
return "Word not found"
print(search(text, word))
so now if word and text is diferent we can call same function again, over and over
+ 3
Now I understand how to do it using function.
def search():
x = "Word found"
y = "Word not found"
if word in text:
return x
else:
return y
x = search()
print (x)
+ 2
Read lection about function again, you just need to place your code inside function and return from it not print directly.
Than call function with input parameter and print returned result
+ 1
Thank you