+ 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))
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
+ 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.
+ 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