0
Error in piece of code
i have the following piece of code: text = input() word = input() def search(x,y): findings=0 for z in range(0, len(x)): if x[z:z+len(x)] == y: print("Word found") findings +=1 else: pass if findings == 0: print("Word not found") else: pass print(search(text, word)) You may recognize it as part of a python challenge. It is trying to find if a word is in a text or not, if there is presenting 'word found' and if not 'word not found'. It is just not happening, and I cannot see the problem in the code. Help please!
2 Antworten
+ 1
Hello Kamodo
I am not a pythonist, but this statement helps to see your problem:
print(x[z:z+len(x)])
It cuts the first letter.
Hello my friend
ello my friend
llo my friend
...
and so on.
In this sample text you are able to find the word "friend" but not "my".
I would say you need a method which splits your text by spaces so that you get each word:
hello my friend -> hello, my, friend
Hope this helps.
+ 1
Thank you, this is very helpful, steps are being made!