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

4th Mar 2021, 6:34 AM
Kirubaananthan Singaravalu
Kirubaananthan Singaravalu - avatar
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))
4th Mar 2021, 7:02 AM
ChaoticDawg
ChaoticDawg - avatar
0
There is an error with the else statement. It outputs word found for all statement
4th Mar 2021, 7:09 AM
Kirubaananthan Singaravalu
Kirubaananthan Singaravalu - avatar
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:
4th Mar 2021, 7:15 AM
ChaoticDawg
ChaoticDawg - avatar