I made a program for fun to find the word 'love' in a sentence and list out how many 'love's there are. As you can see there are several requirements, the 'love' must be a word. That means it has to be separated by spaces or end in punctuation marks. I also account for 'love' starting at the start of the sentence.
num_loves = 0
inpt = input('type ')
for star in range(len(inpt) - 3):
if inpt[star] == 'l' or 'L' and inpt[star+1] == 'o' and inpt[star+2] == 'v' and inpt[star+3] == 'e':
if inpt == 'love' or 'Love':
num_loves = num_loves + 1
elif star == 0 and inpt[star+4] == ' ' or '.' or ',' or '!' or '?':
num_loves = num_loves + 1
elif star != 0:
if inpt[star-1] == ' ' and inpt[star+4] == ' ' or '.' or ',' or '!' or'?':
num_loves = num_loves + 1
if num_loves != 0:
print(num_loves)
print('true')
if num_loves == 0:
print('false')
Although it's great at spotting 'love', it's too great. When I input 'Iloveyou' it still found the word love even though I don't want it to. This is exactly the reason why I input the code "inpt[star-1] == ' ' ". It seems like python ignored this part. Plz help. I'm using python 3.