Here, I have a list of ints, strings, and lists:
from types import *
import re
l1 = [3, 'c', 'thisshouldendloop', 'e', ['h', 2, 'p', 'g'], 7, 4, 5]
I want a while loop which prints each list element and stops when it encounters any string containing 'end'. Before stopping, it should print the string containing 'end', as well. Here's what I have:
length = len(l1)
index = 0
while index < length:
for s in l1:
if (type(s) == 'int'):
print(s)
index += 1
elif (type(s) == 'string'):
if re.search('end', s):
print(s)
# attempts to end while loop by making index > length
index += (length * 2)
else:
print(s)
index += 1
else:
print(s)
index += 1
Output:
3
c
thisshouldendloop
e
['h', 2, 'p', 'g']
7
4
5
What output should look like:
3
c
thisshouldendloop