+ 1

Code Engine in Python for Beginners

Hey guys, My code for the given task is the following but for any reason thats not 100% correct. Can anybody explain me why and help me? :) def search(text,word): for x in range (0 , len(text)): if text[x] == word[0]: i = 1 for y in range(1 , len(word)): if text[x + i] == word[y]: i += 1 if word[len(word)-1] == text[x + len(word)-1]: return("Word found") if x == len(text)-1: return("Word not found") text = input() word = input() print(search(text, word))

30th Mar 2023, 12:45 PM
Kevin S
Kevin S - avatar
3 Answers
+ 6
Your code is too complicated, keep it simple. For this code practice, I don't think you even need a function, you can ask for 2 separate inputs and then see if required word is inside an initial text
30th Mar 2023, 1:15 PM
Lamron
Lamron - avatar
+ 6
Have a look at the previous lesson again and pay attention to the "in" operator: It checks if a substring is _in_ a string. Please do not give ready-made code without any explanation.
30th Mar 2023, 2:09 PM
Lisa
Lisa - avatar
+ 5
def search(text, word): if word in text: print('Word found') else: print("Word not found") text = input() word = input() search(text, word) Try this code
30th Mar 2023, 12:47 PM
Belal Mohamed Salah