I'm trying to write a while loop that goes through a certain list looking for certain substrings. It will find words with those substrings and print out those strings.
Here is a example that works perfectly but for only one word:
lista = ['applepie','appleseed','bananacake','bananabread']
i = 0
z = len(lista)
while i < z:
if ('pie' in lista[0+i]) == True:
print(lista[0+i])
break
i = i + 1
else:
print('Not There Yet')
This prints out applepie (which is what is desired!).
How do I go about fixing this while loop to add in multiple constraints?
I'm trying to do this:
lista = ['applepie','appleseed','bananacake','bananabread']
i = 0
z = len(lista)
while i < z:
if ('pie' in lista[0+i]) == True:
print(lista[0+i])
if ('cake' in lista[0+1]) == True:
print(lista[0+i])
i = i + 1
else:
print('Not There Yet')
This prints out: applepie Not There Yet
When I want this to print out: applepie bananacake
I used multiple 'if' statements, because I know if I want to use an 'elif', it will only run if the first 'if' statement is false.
Any help is appreciated!
'cake' in lista[0+1]should be'cake' in lista[i](not sure why you're adding 0... but it should bei, not1)forloop?!?