I am just learning Python and I want to check if a list contains a word. When I check for the word it always returns 0, even though the function can find it and print it. But the if/else statement always return 0, when I use 2 returns as below. Can you help me?
def line_number(text, word):
"""
Returns the line number (beginning with 1) where the word appears for the first time
:param text: Text in which the word is searched for
:param word: A word to search for
:return: List of Line Numbers
"""
lines = text.splitlines()
i = 0
for line in lines:
i = i +1
if word in line:
return i
else:
return 0
# print("nope")
words = ['erwachet', 'Brust', 'Wie', 'Ozean', 'Knabe']
for word in words:
num = line_number(wilhelm_tell, word)
if num > 0:
print(f"Das Word {word} findet sich auf Zeile {num}.")
else:
print(f"Das Wort {word} wurde nicht gefunden!")
wilhelm_tell?word?