0
Why does it say syntax error on line 3
def search(text, word): if(text.find(word)): return ("Word found") else: return ("Word not found") text = input() word = input() print(search(text, word))
3 Antworten
0
Your code works fine for me without error, but you should be a bit cleaner with your indentation spacing.
def search(text, word):
if(text.find(word)):
return("Word found")
else:
return("Word not found")
text = input()
word = input()
print(search(text, word))
0
There is an error with the else statement. It outputs word found for all statement
0
Ah, that's because all non-zero numbers are truthy in python. The find() method returns -1 when not found and the first index of the found item otherwise. So this isn't a good way to use the method, but you can add != -1 and make it work.
if text.find(word) != -1: